引用
POST http://localhost:8080/ARP/resouces/publishRes.do 406 (Not Acceptable)
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers
所用服务器:
Apache Tomcat/7.0.54
2.具体实现
@RequestMapping(value ="/publishRes",method= RequestMethod.POST) @ResponseBody public AjaxResultVO publishRes(HttpServletRequest request){ AjaxResultVO resultVo = new AjaxResultVO(); resultVo.setSuccess(false); resultVo.setErrCode("500"); resultVo.setExecMsg("查询成功"); return resultVo; }
3.网上调查
按照问题现象在网上搜了一下类似问题不少,代表性的有以下几篇:
The resource identified by this request is only capable of generating responses with characteristics
Spring MVC + JSON = 406 Not Acceptable
Spring 3 MVC And JSON Example
4.0.9使用了网上所说的:
<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.10</version> </dependency>
\spring-webmvc-4.0.9.RELEASE-sources\org\springframework\web\servlet\view\json\MappingJacksonJsonView.Java
import org.codehaus.jackson.JsonEncoding; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.SerializationConfig;
而4.1.0开始,使用了:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.1</version> </dependency>
spring-webmvc-4.1.0.RELEASE-sources\org\springframework\web\servlet\view\json\AbstractJackson2View.java
import com.fasterxml.jackson.annotation.JsonView; import com.fasterxml.jackson.core.JsonEncoding; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature;
6.解决问题
引入fasterxml的jar包,改回spring4.1.4,问题解决。
7.后记:就在要发表文章的时候,在下方预览中看到一篇
Spring MVC 4.1.4 RESTFUL风格返回JSON数据406错误处理
参考:http://blog.csdn.net/kingof007/article/details/44237043
在spring4.X中删除了啊MappingJacksonHttpMessageConverter类文件,可以使用MappingJackson2HttpMessageConverter替换但是启动仍然会报异常
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException 解决办法是在pom文件中引入一下jar包
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.5.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.1</version> </dependency>
参考:http://blog.csdn.net/guying4875/article/details/50314397