Spring MVC 集成 jackson-dataformat-xml 问题
来源:互联网 发布:vb.net int 编辑:程序博客网 时间:2024/05/16 04:45
Spring MVC 集成 jackson-dataformat-xml 问题
HttpMessageNotWritableException
Could not write content
注:如果你没有遇到这个问题,你可以直接看下面解决方法二。
当我在SpringBoot集成Spring MVC中使用XML格式输出的时候,出错了,后台错误信息如下:
Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException:
Could not write content: Not implemented (through reference chain: org.github.abel533.springboot.model.Country[“id”]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Not implemented (through reference chain: org.github.abel533.springboot.model.Country[“id”])
页面提示错误如下:
<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id="created">Sun Dec 27 10:35:49 CST 2015</div><div>There was an unexpected error (type=Internal Server Error, status=500).</div><div>Could not write content: Not implemented (through reference chain: org.github.abel533.springboot.model.Country["id"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Not implemented (through reference chain: org.github.abel533.springboot.model.Country["id"])</div></body></html>
在默认情况下产生错误的原因只有一个,那就是启用了下面的配置:
spring.jackson.serialization.indent_output=true
这个配置会让JSON格式化输出,方便阅读。但是这个配置对XML会起到负面作用。
当输出XML的时候会使用DefaultXmlPrettyPrinter
,这个类在调用下面方法输出XML的时候
public void writeLeafElement(XMLStreamWriter2 sw, String nsURI, String localName, int value) throws XMLStreamException
会使用Stax2WriterAdapter
类的下面方法进行输出:
public void writeRaw(String text, int offset, int len) throws XMLStreamException{ // There is no clean way to implement this via Stax 1.0, alas... throw new UnsupportedOperationException("Not implemented");}
由于这个方法没有实现,这就导致了上面错误的产生。
解决办法
方法一
由于使用下面的配置:
spring.jackson.serialization.indent_output=true
产生的问题,所以只要不启用格式化输出(默认false
)就不会有这个问题。
方法二
参考:https://github.com/FasterXML/jackson-dataformat-xml#maven-dependency
在官方github中写了:
Also: you usually also want to make sure that XML library in use is Woodstox since it is not only faster than Stax implementation JDK provides, but also works better and avoids some known issues like adding unnecessary namespace prefixes.
jackson-dataformat-xml
默认使用下面的stax2-api
依赖:
<dependency> <groupId>org.codehaus.woodstox</groupId> <artifactId>stax2-api</artifactId> <version>3.1.4</version></dependency>
stax2-api
的Stax2WriterAdapter
有些未完成的实现,还有其他的问题。并且Woodstox
比Stax
快,所以官方推荐使用woodstox-core-asl
(Spring官方也推荐这个):
<dependency> <groupId>org.codehaus.woodstox</groupId> <artifactId>woodstox-core-asl</artifactId> <version>4.4.1</version></dependency>
因此我们只要添加上面的woodstox-core-asl
依赖即可解决问题。
- Spring MVC 集成 jackson-dataformat-xml 问题
- jackson-dataformat-xml XXE漏洞(CVE-2016-3720)
- Spring集成Jackson输出非Null数据之XML配置
- XML文本转为MAP对象的简便方法(利用jackson-dataformat-xml)
- spring mvc 结合jackson返回的日期格式问题
- spring mvc json jackson 配置
- 利用jackson-dataformat-csv读写csv文件
- spring spring mvc集成
- spring-mvc注解web.xml配置问题
- spring mvc jackson 支持原理分析
- Spring MVC 4.1.0配置Jackson
- Spring mvc+jackson+jquery 返回json数据出现整合出现包冲突问题
- Spring MVC 使用Jackson转换Mybaits等代理类时的问题
- spring mvc 集成freemarker
- Spring MVC 集成 Velocity
- spring MVC集成Mybatis3
- Spring MVC 集成 Velocity
- spring mvc logback 集成
- linux平台下防火墙iptables原理(转)
- SDUT 数据结构实验之排序二:交换排序
- 终端 指令使用小结
- 关于Eclipse导入新项目中的R文件找不到的问题。
- 判断perl里两个简单hash是否相等的方法
- Spring MVC 集成 jackson-dataformat-xml 问题
- 条件编译基本概念
- iOS开发的22个奇谲巧技,非常实用
- special filter for iptables/netfilter tcpdump/tshark/wireshark
- POJ1160 邮局问题
- 043_字典序最小问题(best cow line)
- 黑马程序员——String类笔记
- 关于上篇文章关于mysql5.7.9上DISABLE KEYS不可用的后续
- iptables防火墙原理详解