Skip to content
Tags

Spring RequestMethod.PATCH

July 20, 2013

Spring WebMVC added support for HTTP PATCH in version 3.2. However there are some gotchas to be aware of that arise from the Java Servlet Specification not handling the HTTP PATCH verb/action.

If you setup a controller method like this:

@RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
public @ResponseBody Candidate update(@PathVariable String id, SomePojo aPojo, BindingResult bindingResult) {

Then the ‘org.springframework.web.method.support.HandlerMethodArgumentResolver‘ that will be used to bind the data will be ‘ServletModelAttributeMethodProcessor‘, but this processor depends on the servlet request returning parameters which it won’t for PATCH requests. For a PATCH request, all that will be available in the native Servlet Request will be the body. Submitting with Content-Type: x-www-form-urlencoded key/values will NOT appear in the ServletRequest.getParameterMap().

If you instead change the controller method to something like this:

@RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
public @ResponseBody Candidate update(@PathVariable String id, @RequestBody SomePojo aPojo, BindingResult bindingResult) {

Then the resolver will instead be RequestResponseBodyMethodProcessor. This method processor has a list of HttpMessageConverters, one of which is MappingJacksonHttpMessageConverter. This converter will properly deserialize the JSON into this POJO if you send the request with “Content-Type: application/json”.

So to summarize, Java Servlet spec doesn’t have a doPatch so it doesn’t parse parameters for it. Instead with Spring you need to pass JSON or some other content type.

Useful Debugging Information

If you are dealing with different content types of have a different needs, below are some links that should aid your debugging efforts.

A breakpoint on HandlerMethodArgumentResolverComposite will let you know which HandlerMethodArgumentResolver is being used.

Another useful thing to inspect is the message converters being used if you are using the @RequestBody annotation that would trigger the RequestResponseBodyMethodProcessor. A breakpoint in AbstractMessageConverterMethodArgumentResolver

Advertisements

From → Java

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: