問題描述:
406 Not Acceptable,message:description The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ()錯誤。(說是指定的資源已經找到,但它的MIME類型和客戶在Accpet頭中所指定的不兼容)。
前后台代碼經過排查都沒有問題,本人在Spring mvc控制層用到了@ResponseBody標注,以便返回的數據為json格式的數據,到前端JS中用。而前端卻需要text/html;charset=UTF-8這樣的格式,導致的問題。
解決辦法如下:
1.需要使用jackson,在pom.xml中引入jackson的依賴
<!-- jackson start --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-lgpl</artifactId> <version>1.9.12</version> </dependency> <!-- jackson end -->
2. spring-servlet.xml文件中配置轉換的bean
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <util:list id="beanList"> <ref bean="mappingJacksonHttpMessageConverter" /> </util:list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean>
3.spring-servlet.xml文件的頭部聲明部分加入如下三行即可(因為util:list)
xmlns:util="http://www.springframework.org/schema/util" http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd