Add JSON message converter for multipart/form-data

前端 未结 2 1979
无人及你
无人及你 2021-02-14 18:06

In my Spring MVC server I want to receive a multipart/form-data request containing both a file (an image) and some JSON metadata. I can build a well-formed multipart request whe

2条回答
  •  庸人自扰
    2021-02-14 18:33

    If you use the @RequestPart annotation instead of @RequestParam, it will actually pass the parameters through the message converters. So, if you change your controller method to the following, it should work as you describe:

    @RequestMapping(value = MY_URL, method=RequestMethod.POST, headers="Content-Type=multipart/form-data")
    public void myMethod(@RequestParam("image") MultipartFile file, @RequestPart("json") MyClass myClass) {
    ...
    }
    

    You can read more about it in the Spring reference guide: http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/mvc.html#mvc-multipart-forms-non-browsers

提交回复
热议问题