Spring Boot controller - Upload Multipart and JSON to DTO

前端 未结 8 1125
南旧
南旧 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:42

    Add the consumer type to your request mapping .it should work fine.

    @POST
    @RequestMapping("/upload")
    public ResponseEntity upload(@RequestParam("file") MultipartFile file,consumes = "multipart/form-data") 
    {
        if (file.isEmpty()) {
            return new ResponseEntity(HttpStatus.BAD_REQUEST);
        } else {
            //...
        }
    }
    
        

    提交回复
    热议问题