You’ve got this Spring @RestController and mapped a URL that contains an email as part of the URL path. You cunningly worked around the dot truncation issue [1] and you are ready to roll. And suddenly, on some URLs, Spring will return a 406 [2] which says that the browser requested a certain content type and Spring can’t serialize the response to that content type. The point is, you’ve been doing Spring applications for years and you did all the MVC declarations right and you included Jackson and basically you are stuck. Even worse, it will spit that error out only on some emails in the URL path, most notably those ending in a “.com” domain.
@RequestMapping(value = "/agenda/{email:.+}", method = RequestMethod.GET) public List checkAgenda(@PathVariable("email") String email)
The issue [3] is quite tricky: the application server performs some content negotiation and convinces Spring that the browser requested a “application/x-msdownload” content, despite that occurring nowhere in the request the browser actually submitted.
The solution is to specify a content negotiation manager for the web application context:
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="defaultContentType" value="application/xml" /> </bean> <mvc:annotation-driven content-negotiation manager="contentNegotiationManager" />
Resources
[1] Spring MVC @PathVariable with dot (.) is getting truncated
http://stackoverflow.com/questions/16332092/spring-mvc-pathvariable-with-dot-is-getting-truncated
[2] spring mvc not returning json content – error 406
http://stackoverflow.com/questions/4069903/spring-mvc-not-returning-json-content-error-406
[3] Tomcat content negotiation
http://www.baeldung.com/2011/10/25/building-a-restful-web-service-with-spring-3-1-and-java-based-configuration-part-2/#comment-1449791556
This solution really helped us. Thanks a lot for putting it up here 🙂
LikeLike
thank you very much
LikeLike
This is why I hate Java / Spring / Hibernate. Too many layers of complexity getting in the way of simple tasks.
LikeLike
I think that the Java ecosystem was built around the ambition of handling complex tasks, which necessarily makes simple tasks look overly complicated.
LikeLike
They don’t just *look* overly complicated, they *are* overly complicated. At any rate, although this article was informative it doesn’t seem to be providing a solution to my problem. I’m still getting 406 errors with a simple AJAX GET to a url with an email address as the last parameter.
LikeLike
Using latest version of Spring (5.2.0) the same solution does not work.
LikeLike