Add JSON message converter for multipart/form-data

前端 未结 2 1971
无人及你
无人及你 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

    0 讨论(0)
  • 2021-02-14 18:42

    i don't have any idea how do it this but i know @RequestParam("json") MyClass myClass u can change to @RequestParam("json") String myClass and build object class by JSON converted! It's not good but it's works

    0 讨论(0)
提交回复
热议问题