Automatic negotiation of ContentType at the ViewResolver (3.0.6 onwards)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Automatic negotiation of ContentType at the ViewResolver (3.0.6 onwards)

I have just upgraded from 3.0.1 to 3.0.9 and have noticed a change in the content types being returned for individual requests. For the most part this is most welcome and actually tidies up a lot of things for me.

However I am having some difficulty with RequestMappings that return non-html output (JSON and XML) with appropriate .json/.xml extensions on the RequestMapping. There are some cases where the request may return a 404 error code due to a missing element referenced by the path.

I do this by throwing an exception and using @ExceptionHandler routines to catch the error and render an appropriate error page. However the 404 error page I generate is a HTML document generated from a Thymeleaf template. This error page always gets the content type of the original path suffix in the RequestMapping which makes things unhappy.

I have generate a small example at that demonstrates the issue.

I'd welcome some input on how to solve this. I have tried directly setting the content type in the HttpServletResponse in the @ExceptionHandler but that seems to be ignored.

I had a quick look at the changes between 3.0.5 and 3.0.6. The relevant code that generates this seems to be in SpringContentTypeUtils#computeViewContentType it seems to be just using the suffix code and I cannot see it trying to use the appropriate content type of the template that is being expanded.