javascript
Spring MVC控制器用@ResponseBody声明返回json数据报406的问题
本打算今天早點下班,結果下午測試調試程序發現一個問題糾結到晚上才解決,現在寫一篇博客來總結下。
是這樣的,本人在Spring mvc控制層用到了@ResponseBody標注,以便返回的數據為json格式的數據,到前端JS中用。
問題來了,當我用火狐調試發現請求總是報: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頭中所指定的不兼容)。
于是樓主那個糾結,那個郁悶啊。BUG了又BUG,百度了又百度,又懷疑是引用的ext.js文件有問題,因為報ext.base和ext.all語法錯誤。最后發現是mime類型不匹配。由于我要返回的是json數據,而瀏覽器接受的是text/html;charset=UTF-8文本類型。不過發現問題還有好幾個,一一解決之:
2.? spring-servlet.xml文件中需要配置轉換的bean
<beanclass="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.? 最后,別忘了還要聲明util的schema文件和地址,在spring-servlet.xml文件的頭部聲明部分加入如下三行即可
xmlns:util="http://www.springframework.org/schema/util"http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-3.0.xsd? OK,搞定,大功告成,下班~~~
?
轉載于:https://www.cnblogs.com/tanglc/p/3664795.html
總結
以上是生活随笔為你收集整理的Spring MVC控制器用@ResponseBody声明返回json数据报406的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用scrollIntoView方法解决
- 下一篇: 让JavaScript回归函数式编程的本