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

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

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