Java – httpmediatypenotacceptableexception after upgrading to spring 3.2

After upgrading the spring MVC application to spring 3.2, when accessing some of my URLs, I receive the following exception:

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
    at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:203) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:272) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:212) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:55) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:297) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1091) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1076) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:896) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) ~[spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915) [spring-webmvc-3.2.0.RELEASE.jar:3.2.0.RELEASE]
(...)

This exception causes HTTP 406 to be unacceptable

I try to create a simple controller with a URL I can't access:

@RequestMapping(value = "/resources/foo.js",produces = "text/javascript")
@ResponseBody
public String foo() throws Exception {
    return "";
}

When I use a normal browser with * / * in accept header, I don't understand why I should get an HTTP 406 It's even more strange that this code is using spring 3.1 2, but not for spring 3.2 Why?

Solution

There have been several changes related to spring doors content negotiations in 3.2 One change is that content negotiation can now be completed according to the file suffix in the URL This feature is enabled by default In spring before 3.2, HTTP accept header was used for content negotiation When the browser accesses your URL content, negotiation rarely has problems, because the browser always sends accept: (...) * / *

Spring has a suffix map = > media type For ". JS", the default media type is "application / x-javascript" When spring tries to find the request to / resources / foo JS handler mapping, it will not match your foo () – method because it produces the wrong media type

I don't know if the spring team has thought about this case At least oddly, it allows you to create a @ requestmapping that cannot be accessed (due to the incompatibility between the. JS media type and the one set in the production field)

There are several ways to solve this problem One is to change the generation parameter to "application / x-javascript" The other is to change the media type of ". JS" to "text / JavaScript" (see the docs of how to do that) The third possibility is to close suffix based content negotiation (again, see the docs of how to do it)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>