I have a Spring MVC application that receives an HTTP request from an external system as a JSON string, and its response is returned similarly to a JSON string. My controller is correctly annotated with @RequestBody and @ResponseBody , and I have integration tests that actually send requests to verify that everything is working as expected.
However, when I went to check my application for the actual external system that will use it, I found that the incoming requests did not indicate the type of content! This completely confuses Spring and leads to the following types of errors:
DEBUG [] 2014-04-17 13:33:13,471 AbstractHandlerExceptionResolver.java:132 resolveException - Resolving exception from handler [ com.example.controller.MyController@1d04f0a ]: org.springframework.web.HttpMediaTypeNotSupportedException: Cannot extract parameter (ValidationRequest request): no Content-Type found
So, is there a way to get Spring to forward such a request through the MappingJacksonHttpMessageConverter , either somehow forcing Spring to use a custom handler chain, or modifying the incoming request to explicitly set the content type
I have tried several things:
- The
MappingJacksonHttpMessageConverter extension MappingJacksonHttpMessageConverter that its canRead() and canWrite() methods always return true. Unfortunately, Spring doesn't even get to look at message converters before releasing it due to lack of content type. - Use interceptors or servlet filters to manually set the type of content. Unfortunately, I do not see a way for any of these mechanisms to make changes to the incoming request, besides setting new attributes.
Any ideas are welcome.
To answer the comments below, my @RequestMapping looks like this:
@RequestMapping(value="/{service}" ) public @ResponseBody MyResponseObject( @PathVariable String service, @RequestBody MyRequestObject request) {
So there is nothing here that JSON indicates, but without the content type, Spring doesn't seem to even strike at creating my request object from an incoming request (which makes sense since it does not have enough information to determine how to do this).
As for the @geoand comment, asking βwhy can't you add the header of the http type of content to the servlet filter or the <w760 interceptor>, the answer isβ because I am dumb and forgot how the servlet filters work. βThis is an approach that I ultimately used to solve the problem, which I will add as an answer soon.