Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add JSON message converter for multipart/form-data

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 where the JSON section has Content-Type=application/json. The Spring service is in the form:

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

The file is correctly uploaded, but I'm having problems with the JSON part. I get this error:

org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'myPackage.MyClass'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [myPackage.MyClass]: no matching editors or conversion strategy found

If I don't use multipart request JSON conversion works well using Jackson 2, but when using multipart I get the previous error. I think I have to configure the multipart message converter to support JSON as part of the message, but I don't know how. Here is my configuration:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<mvc:annotation-driven>
    <mvc:message-converters>
         <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
    </mvc:message-converters>
</mvc:annotation-driven>

All works well if I use String as type of myClass instead of MyClass, but I want to use the Spring MVC support for parameter conversion.

like image 562
user1781028 Avatar asked Jul 26 '13 15:07

user1781028


People also ask

How do I pass a multipart file in JSON?

To pass the Json and Multipart in the POST method we need to mention our content type in the consume part. And we need to pass the given parameter as User and Multipart file. Here, make sure we can pass only String + file not POJO + file. Then convert the String to Json using ObjectMapper in Service layer.

What is multipart JSON?

Multipart requests combine one or more sets of data into a single body, separated by boundaries. You typically use these requests for file uploads and for transferring data of several types in a single request (for example, a file along with a JSON object).

What is HttpMessageConverter?

HttpMessageConverter is a strategy interface that specifies a converter that can convert from and to HTTP requests and responses in Spring REST Restful web services. Internally Spring MVC uses it to convert the Http request to an object representation and back.


2 Answers

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

like image 184
Jasper Aarts Avatar answered Oct 23 '22 22:10

Jasper Aarts


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

like image 35
Denis Avatar answered Oct 23 '22 21:10

Denis