(1)错误:
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request “accept” headers.
这分明是说我的请求资源有响应,但是呢,请求消息头headers里的Accept并不能接受这种响应。
通俗一点:我服务器返回的是json数据,但是跟我请求的数据类型不同[Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8].
我的controller方法,如下图
用eclipse的Monitor监视结果,如下图
(2)解决办法:web.xml我使用 url-pattern拦截所有以html结尾的请求,而SpringMVC规定:在SpringMVC中如果请求以html结尾,那么就不会返回JSON数据。
改成.do就行了
返回的json数据在网页上展示
除此之外,此问题的还有其他原因,最主要的就是看jackson依赖的jar包有没有冲突。
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.9</version>
</dependency>
(3)查找了几个参考的博文和论坛:
1.Http请求中Content-Type讲解以及在Spring MVC中的应用
2.Spring WebMVC 4.1返回json时导致的 406(Not Acceptable)问题
3. json转换错误:No converter found for return value of type
4.Spring MVC 4.1.4 RESTFUL风格返回JSON数据406错误处理
5.Spring MVC + JSON = 406 Not Acceptable