Spring MVC 集成 jackson-dataformat-xml 问题
2015-12-27 11:25
435 查看
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”])
页面提示错误如下:
[code]<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的时候
[code]public void writeLeafElement(XMLStreamWriter2 sw, String nsURI, String localName, int value) throws XMLStreamException
会使用
Stax2WriterAdapter类的下面方法进行输出:
[code]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依赖:
[code] <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官方也推荐这个):
[code]<dependency> <groupId>org.codehaus.woodstox</groupId> <artifactId>woodstox-core-asl</artifactId> <version>4.4.1</version> </dependency>
因此我们只要添加上面的
woodstox-core-asl依赖即可解决问题。
相关文章推荐
- MyEclipse激活步骤
- 【Java并发编程实战】----- AQS(三):阻塞、唤醒:LockSupport
- Java随机数总结
- 使用axis1+spring+hibernate搭建webservice框架
- Java Socket(六) 使用多线程实现多客户端的通信
- MyEclipse8.5注册码,到2018年
- Pentaho技术白皮书中文版(一)----用 Eclipse 构建和调试 Pentaho
- Java内存溢出详解
- 关于Eclipse导入新项目中的R文件找不到的问题。
- Spring声明式事务异常回滚机制
- 使用Google Roads API抓取道路信息(java实现)
- 从jdk源码角度理解jvm类加载机制
- [图说Java]图说String的不变性
- JAVA常用类之格式化
- Java Socket(五) 服务器响应客户端
- 《Java Web程序开发入门》知识总结(三)
- 一张图让你看清Java集合类(Java集合类的总结)
- Java的JDBC中Statement与CallableStatement对象实例
- 记JAVA的二三事(2)——JVM存储区+hashCode()和equals()的隐式约定
- Java_log4j_使用开源日志记录工具log4j