<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.5</version>
</dependency>
@JacksonXmlRootElement:指定生成xml根标签的名字
属性: namespace,localName
@JacksonXmlProperty:指定包装标签名,或者指定标签内部属性名
属性: namespace,localName,isAttribute(default: false)
@JacksonXmlElementWrapper:可用于指定List等集合类,外围标签名
属性: namespace,localName,useWrapping (default:true)
@JacksonXmlText:指定当前这个值,没有xml标签包裹。
属性: namespace,localName
实体类转XML
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import lombok.Data;
import java.io.Serializable;
* @Author by peng .
* @Descriptions
* @Datetime in 2018/5/18 08:05.
@Data
@JacksonXmlRootElement(localName = "request")
public class RequestXml<T> implements Serializable {
private static final long serialVersionUID = 1L;
private String request;
@JacksonXmlProperty(isAttribute = true)
private String service;
@JacksonXmlProperty(isAttribute = true)
private String method;
@JacksonXmlProperty
private String token;
@JacksonXmlProperty
private T content;
复制代码
工具类
public static String objectToXml(Object object) throws IOException {
xmlMapper.setDefaultUseWrapper(false);
xmlMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
xmlMapper.enable(MapperFeature.USE_STD_BEAN_NAMING);
String resultXml = xmlMapper.writeValueAsString(object);
return resultXml;
复制代码
测试
public static void test2() throws IOException {
RequestXml<Dept> requestXml = new RequestXml<>()
Dept dept = new Dept()
dept.setDeptCode("12345")
dept.setDeptName("开发部")
requestXml.setToken("adjfa
requestXml.setContent(dept)
requestXml.setService("DeptService")
requestXml.setMethod("getDeptList")
String xml = JsonUtil.objectToXml(requestXml)
log.info(xml)
复制代码
输出
<request service="DeptService" method="getDeptList">
<token>adjfa;fjlaksdjfasdfiew</token>
<content>
<deptCode>12345</deptCode>
<deptName>开发部</deptName>
</content>
</request>
xml转实体类
public static <T> T xmlToObject(String xml,Class<T> clazz) throws IOException, XMLStreamException {
xmlMapper.setDefaultUseWrapper(false);
xmlMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
xmlMapper.enable(MapperFeature.USE_STD_BEAN_NAMING);
return xmlMapper.readValue(xml, clazz);
复制代码
实体类
@Data
@JacksonXmlRootElement(localName = "response")
public class ResponseData {
@JacksonXmlProperty
private String errorCode;
@JacksonXmlProperty
private String errorDesc;
@JacksonXmlProperty
private ResponseResult result;
@Data
@JacksonXmlRootElement(localName = "result")
public class ResponseResult {
@JacksonXmlProperty(localName = "lbsLocRecord")
private List<LbsLocRecord> lbsLocRecord;
@Data
@JacksonXmlRootElement(localName = "lbsLocRecord")
@EqualsAndHashCode(callSuper = false)
@TableName("lbs_loc_record")
public class LbsLocRecord extends BaseEntity {
@TableId(value = "locate_record_id", type = IdType.ID_WORKER)
private Long locateRecordId;
private String deviceId;
private String jmx;
private String jmy;
private String lon;
private String lat;
private String time;
private String position;
@TableField(exist = false)
private String errcode;
@TableField(exist = false)
private String errmsg;
@TableField(exist = false)
private boolean timeOut;
复制代码
测试类
public static void testXmlToObject() throws IOException, XMLStreamException {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root>\n" +
" <errorCode>00000</errorCode>\n" +
" <errorDesc></errorDesc>\n" +
" <result>\n" +
" <lbsLocRecord>\n" +
" <deviceId>15054916000</deviceId>\n" +
" <jmx>0</jmx>\n" +
" <jmy>0</jmy>\n" +
" <lon>127.16732</lon>\n" +
" <lat>44.93510</lat>\n" +
" <time>2018-05-18 14:06:03</time>\n" +
" <position>黑龙江省哈尔滨市五常市金山公园五常镇金山屯;G1211/S222/亚臣路;金山大街/G1211(路口);金山水保科技园附近</position>\n" +
" <timeOut>false</timeOut>\n" +
" </lbsLocRecord>\n" +
" <lbsLocRecord>\n" +
" <deviceId>15244366000</deviceId>\n" +
" <jmx>0</jmx>\n" +
" <jmy>0</jmy>\n" +
" <lon>0.0</lon>\n" +
" <lat>0.0</lat>\n" +
" <errcode>9990240</errcode>\n" +
" <errmsg>LBMP_CALLEE_NO_EXCEPTION</errmsg>\n" +
" <time>2018-05-18 14:25:58</time>\n" +
" <position></position>\n" +
" <timeOut>false</timeOut>\n" +
" </lbsLocRecord>\n" +
" </result>\n" +
"</root>\n";
ResponseData responseData = JsonUtil.xmlToObject(xml, ResponseData.class);
String errorCode = responseData.getErrorCode();
if ("00000".equals(errorCode)) {
List<LbsLocRecord> resultList = responseData.getResult().getLbsLocRecord();
for (LbsLocRecord record : resultList){
if (StringUtils.isBlank(record.getErrcode())){
log.info("插入数据");
String string = JSON.toJSONString(responseData);
log.info("responseString:"+ string );
log.info("responseData:"+ JSON.parse(string));
安装掘金浏览器插件
多内容聚合浏览、多引擎快捷搜索、多工具便捷提效、多模式随心畅享,你想要的,这里都有!
前往安装