spring mvc处理json类型返回结果异常
来源:互联网 发布:圣元 淘宝网折 编辑:程序博客网 时间:2024/05/16 18:11
晚上弄个项目骨架出了点小插曲,发发牢骚……
异常情况:
一请求就是错误:The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request “accept” headers.
http 406,请求头的accept不匹配,主要是在spring mvc中使用了@ResponseBody
解决:
缺少两个必备的包:
<dependency> <groupId >org.codehaus.jackson </groupId > <artifactId >jackson-mapper-asl </artifactId > <version >1.9.13 </version > </dependency > <dependency > <groupId >org.codehaus.jackson </groupId > <artifactId >jackson-core-asl </artifactId > <version >1.9.13 </version > </dependency >
加入这两个依赖包即可解决。
过程:
虽然以前就了解过这个可能其中一个原因,但开始并不认为是这个,因为觉得好扯,缺少包为什么不是报异常而已报这样的错误误导别人?
所以我详细了看了一下项目的配置,因为以前这类应用是别人整的,所以报了怀疑的态度,还一路追踪了下spring校验mediatype是否匹配的过程,还是发现并没有什么问题。
这。 只能是ReponseBody的锅了,在最后将返回结果转化成json的时候错了,去看了一眼项目的依赖包,还真没有jackson相关的包,所以加了一下上面两个jar的依赖,还真OK了。
最终还是决定去看看源码为什么这里错误提示差这么多。 我们知道输入输出的转换需要通过HttpMessageConverter来进行,spring需要依赖jackson来做json与java对象的转换,所以直接看jackson相关的converter即可,如:
if (jackson2XmlPresent ) { ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.xml().applicationContext( this .applicationContext ).build(); messageConverters .add(newMappingJackson2XmlHttpMessageConverter( objectMapper )); } else if ( jaxb2Present) { messageConverters .add(newJaxb2RootElementHttpMessageConverter()); } if (jackson2Present ) { ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().applicationContext( this .applicationContext ).build(); messageConverters .add(newMappingJackson2HttpMessageConverter(objectMapper )); }
原来不是一定启用,那在看看判断条件:
private static final boolean jackson2XmlPresent = ClassUtils. isPresent( "com.fasterxml.jackson.dataformat.xml.XmlMapper" , WebMvcConfigurationSupport.class .getClassLoader());
醉了,只有确定相关的类存在才添加相应的converter,也不会做啥提示。
本文转载自:spring mvc处理json类型返回结果异常
0 0
- spring mvc处理json类型返回结果异常
- spring mvc统一处理异常时候返回json或xml
- Spring MVC--18.json结果返回(@)
- Spring MVC--18.结果返回(@json)
- spring mvc返回json 类型的 数据
- spring mvc 返回json类型设置
- Spring MVC 全局异常处理-RESTAPI接口返回统一JSON格式-自定义异常处理--404异常捕捉
- spring mvc 返回json 时间格式处理
- spring mvc 输出 json 异常处理
- spring mvc 输出 json 异常处理
- Spring MVC 的的异常处理(跳转页面和返回json双模式)
- Spring MVC全局异常后返回JSON异常数据
- Spring MVC全局异常后返回JSON异常数据
- Spring MVC全局异常后返回JSON异常数据
- Spring MVC全局异常后返回JSON异常数据
- .net mvc Angular2项目 ajax请求返回结果异常处理
- Spring MVC返回JSON
- spring MVC返回JSON
- NW.js & Electron Compared (2016 Edition)
- 一个简单的通过代码请求k8s生成应用的demo
- 使用JDBC连接mysql,db2等数据库
- 追求Masonry
- HTML5新标签在IE6/IE7/IE8中的几种兼容方式
- spring mvc处理json类型返回结果异常
- js判断单选按钮是否被选中
- 自动保存用户名密码功能:autocomplete详解
- 第13章 线程安全与锁优化
- MSSQL exists与not exists; in与not in的区别
- 友元
- python paramiko模块安装和使用
- Web前端程序员推荐7个实用的JavaScript工具
- 14.进程间通信---网络通信(udp协议)