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.