jackson-处理xml
jackson是springboot默认集成的json库,但是它不单单是json库,它还可以用来处理xml
依赖
需要单独引入jackson的xml格式化模块
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.11.4</version>
</dependency>
使用
xml和json互相转换的流程都是先转换为java对象,然后再转为目标类型
XmlMapper
对应与转换json用的ObjectMapper
-
默认配置
XmlMapper xmlMapper = new XmlMapper();
-
自定义配置
XmlMapper.builder() .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false)//忽略实体类没有对应属性,true抛异常 .serializationInclusion(JsonInclude.Include.NON_NULL)//忽略null .propertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE)//驼峰首字母小写 .build();
pojo转xml
Stringn userXml = xmlMapper.writeValueAsString(user)
xml转pojo
User userPojo = xmlMapper.readValue(userXml, User.class);