Spring MVC REST controller says 406 when emails are part URL path

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" />

<mvc:annotation-driven content-negotiation manager="contentNegotiationManager" />


[1] Spring MVC @PathVariable with dot (.) is getting truncated

[2] spring mvc not returning json content – error 406

[3] Tomcat content negotiation

6 thoughts on “Spring MVC REST controller says 406 when emails are part URL path

  1. This is why I hate Java / Spring / Hibernate. Too many layers of complexity getting in the way of simple tasks.


      1. 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.


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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.