I am trying to build a Spring MVC controller which will receive a POSTed form with a parameter in JSON format, and have Spring automatically convert it to a Java object.
Spring invokes your @RequestMapping
methods with reflection. To resolve each argument it's going to pass to the invocation, it uses implementations of HandlerMethodArgumentResolver
. For @RequestParam
annotated parameters, it uses RequestParamMethodArgumentResolver
. This implementation binds a request parameter to a single object, typically a String
or some Number
type.
However, your use case is a little more rare. You rarely receive json
as a request parameter, which is why I think you should re-think your design, but if you have no other choice, you need to register a custom PropertyEditor
that will take care of converting the request parameter's json
value into your custom type.
Registration is simple in an @InitBinder
annotated method in your @Controller
class
@InitBinder
public void initBinder(WebDataBinder dataBinder) {
dataBinder.registerCustomEditor(MyMessage.class, new PropertyEditorSupport() {
Object value;
@Override
public Object getValue() {
return value;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
value = new Gson().fromJson((String) text, MyMessage.class);
}
});
}
In this particular case, we don't need all the methods of the PropertyEditor
interface, so we can use PropertyEditorSupport
which is a helpful default implementation of PropertyEditor
. We simply implement the two methods we care about using whichever flavor of JSON parser we want. I used Gson
because it was available.
When Spring sees that it has a request parameter that you requested, it will check the parameter type, find the type MyMessage
and look for a registered PropertyEditor
for that type. It will find it because we registered it and it it will then use it to convert the value.
You might need to implement other methods of PropertyEditor
depending on what you do next.
My recommendation is to never send JSON as a request parameter. Set your request content type to application/json
and send the json
as the body of the request. Then use @RequestBody
to parse it.
You can also use @RequestPart
like this:
@RequestMapping(value = "/issues", method = RequestMethod.POST, headers = "Content-Type=multipart/form-data")
public String uploadIssue(@RequestParam("image") MultipartFile file, @RequestPart("issue") MyMessage issue)