Skip to content

Invalid MIME-type causes a 500 error [SPR-7712] #12368

@spring-projects-issues

Description

@spring-projects-issues

Brett opened SPR-7712 and commented

I have a REST service setup with the ContentNegotiatingViewResolver to transform responses into either xml or json based on the Accept header. I set the useNotAcceptableStatusCode property to true and it appears to work as expected. However, if the Accept header is a completely invalid media type like 'foo' then an exception is thrown and the consumer ends up getting back a 500 Internal Server Error instead of the expected 406. The top of the resulting stack trace is included below:

SEVERE: Servlet.service() for servlet Spring MVC Dispatcher Servlet threw exception
java.lang.IllegalArgumentException: "foo" does not contain '/'
at org.springframework.http.MediaType.parseMediaType(MediaType.java:562)
at org.springframework.http.MediaType.parseMediaTypes(MediaType.java:602)
at org.springframework.web.servlet.view.ContentNegotiatingViewResolver.getMediaTypes(ContentNegotiatingViewResolver.java:306)
at org.springframework.web.servlet.view.ContentNegotiatingViewResolver.resolveViewName(ContentNegotiatingViewResolver.java:366)
at org.springframework.web.servlet.DispatcherServlet.resolveViewName(DispatcherServlet.java:1078)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1027)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.cerner.net.oauth.j2ee.filter.OAuthValidatorFilter$1.run(OAuthValidatorFilter.java:215)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:396)
at com.cerner.net.oauth.j2ee.filter.OAuthValidatorFilter.doFilter(OAuthValidatorFilter.java:213)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
...

Considering that an invalid media type is a problem with the client making the request to the service, I think it should return a proper 406 response if the useNotAcceptableStatusCode flag is set.


Affects: 3.0.4, 3.0.5

Reference URL: http://forum.springsource.org/showthread.php?p=317618#post317618

Referenced from: commits cb0c0c5, 0a48936

Backported to: 3.0.6

4 votes, 6 watchers

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: backportedAn issue that has been backported to maintenance branchestype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions