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-apiStax2WriterAdapter有些未完成的实现,还有其他的问题。并且WoodstoxStax快,所以官方推荐使用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 问题相关推荐

  1. Spring MVC集成slf4j-logback

    转自: Spring MVC集成slf4j-logback 1.  Spring MVC集成slf4j-log4j 关于slf4j和log4j的相关介绍和用法,网上有很多文章可供参考,但是关于logb ...

  2. Spring MVC集成slf4j-logback - 我想跟代码谈谈 - 博客频道 - CSDN.NET

    Spring MVC集成slf4j-logback - 我想跟代码谈谈 - 博客频道 - CSDN.NET

  3. Spring MVC集成Tiles使用方法

    Spring MVC集成Tiles使用方法 转载于:https://www.cnblogs.com/zhujiabin/p/5012129.html

  4. Spring MVC集成Swagger2.0

    在集成Swagger之前,得先说说什么是Swagger,它是用来做什么的,然后再讲讲怎么集成,怎么使用,当然,在这之前,需要了解一下OpenAPI. OpenAPI OpenAPI 3.0规范定义了一 ...

  5. Spring MVC集成Spring Data Reids和Spring Session实现Session共享

    说明:Spring MVC中集成Spring Data Redis和Spring Session时版本是一个坑点,比如最新版本的Spring Data Redis已经不包含Jedis了,需要自行引入. ...

  6. Spring MVC集成Log4j

    以下示例显示如何使用Spring Web MVC框架集成LOG4J.首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序: 创建一 ...

  7. jqgrid mvc_jqGrid,REST,AJAX和Spring MVC集成

    jqgrid mvc 两年多以前,我写了一篇关于如何在Struts2中实现优雅的CRUD的文章. 实际上,我必须就该主题写两篇文章,因为该主题如此广泛. 今天,我采用了一套更为流行的,完善的框架和库, ...

  8. jqGrid,REST,AJAX和Spring MVC集成

    两年多以前,我写了一篇关于两个如何在Struts2中实现优雅的CRUD的文章. 实际上,我必须就该主题写两篇文章,因为该主题如此广泛. 今天,我采用了一套更为流行的,完善的框架和库,采用了更为轻量级的 ...

  9. Spring mvc集成log4j2

    前期环境 Spring mvc + Maven + Idea 一.下面开始配置log4j2,先简单演示其如何配置,再仔细了解log4j2用法. 1.1  配置pom.xml,引用log4j2相关包 & ...

最新文章

  1. 1014 装箱问题 CODE[VS]
  2. 皮一皮:爱迪生看了流泪,特斯拉看了沉默...
  3. dobbo 管理端端口_安装 Dubbo 管理控制台
  4. php 获取鼠标的坐标,如何实时获取鼠标的当前坐标-
  5. 探索 OpenStack 之(10):深入镜像服务Glance
  6. 如何在Java中实现线程池
  7. ChinaJoy是什么
  8. 改变维度_一个人的各种维度,哪些是可以改变的?
  9. mysql报错:ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
  10. 基于matlab的信号频谱分析 开题报告,基于MATLAB的数字信号处理开题报告
  11. SpringBoot实现人脸识别功能
  12. Web 攻防第一阶段 I-03:收集 Web 站点信息
  13. 一文了解同态加密(Homomorphic Encryption, HE)
  14. XyplayerX4.0 影视解析源码+解析接口+安装步骤
  15. 【2】数据可视化:基于 Echarts + Python 实现的动态实时大屏 - 物流大数据
  16. 在html中frame标签的作用,HTML中的frame标签常见的6大属性,新手一定要悉知!
  17. 找寻自己的哲学世界?
  18. 网络推广和网络营销的区别
  19. 关于“A Native Collection has not been disposed, resulting in a memory leak. Enabl”问题
  20. 倾斜模型精细化处理_倾斜摄影与地面激光点云融合精细化建模研究

热门文章

  1. Spring 框架面试题总结
  2. linux开启关闭Redis
  3. Android 获取系统壁纸
  4. Canvas-04-Konva
  5. el-upload 上传图片文件方法
  6. vue 日期面板_Vue实战:日期选择器
  7. Hbuilder用自有证书打包 ios App上架AppStore流程
  8. MySQL 的 MVCC 原理详解
  9. Xcode11 Application Loader altool上传ipa
  10. Java读取.ipa程序下info.plist