HttpMediaTypeNotAcceptableException after upgrading to Spring 3.2

前端 未结 4 1865
孤独总比滥情好
孤独总比滥情好 2021-02-08 15:23

After upgrading my Spring MVC application to Spring 3.2 I\'m getting the following exception when accessing some of my URLs:

org.springframework.web.HttpMediaTyp         


        
4条回答
  •  余生分开走
    2021-02-08 16:15

    There have been several changes related to how Spring does content-negotiations in 3.2. One of these changes is that content negotiations can now be done based on the file suffix in the URL. This feature is enabled by default. In Spring versions prior to 3.2 the HTTP accept-header were used for content-negotiations. When browsers accessed your URLs content-negotiation was seldom an issue, as browser always sends Accept:(...)*/*.

    Spring has a map of suffix => Media type. For ".js" the default media type is "application/x-javascript". When Spring tries to lookup the handler mapping for a request to /resources/foo.js, it won't match your foo()-method as it produces the wrong media type.

    I'm not sure if the Spring team has thought through this case. It is at least a bit strange that it lets you create a @RequestMapping which cannot be accessed (because of the incompatibility between the .js-media type and what is set in the produces field).

    There are several ways of fixing this issue. One is to change the produces-parameter to "application/x-javascript". Another would be to change the media type of ".js" to "text/javascript" (see the docs of how to do that). A third possibility is to turn off content-negotiations based on suffixes (again, see the docs of how to do it).

提交回复
热议问题