Spring Boot controller - Upload Multipart and JSON to DTO

前端 未结 8 1134
南旧
南旧 2020-11-28 05:16

I want to upload a file inside a form to a Spring Boot API endpoint.

The UI is written in React:

export function createExpense(formData) {
  return         


        
8条回答
  •  醉酒成梦
    2020-11-28 05:41

    @RequestMapping(value = { "/test" }, method = { RequestMethod.POST })
    @ResponseBody
    public String create(@RequestParam("file") MultipartFile file, @RequestParam String description, @RequestParam ArrayList sharers) throws Exception {
        ExpensePostDto expensePostDto = new ExpensePostDto(file, description, sharers);
        // do your thing
        return "test";
    }
    

    This seems to be the easiest way out here, other ways could be to add your own messageConverter.

提交回复
热议问题