1、POM 依賴
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
2、轉換工具類
package com.tencent.iov.qw.utils;
import com.google.common.base.Charsets;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
/**
* XML處理工具
*
* @author YG
* @date 2020/01/27
*/
public class XmlParseUtill {
/**
* java對象轉xml
*
* @param obj 對象
*
* @return xml
*/
public static String toXml(Object obj) {
Map<String, Object> props = new HashMap<>(5);
// 是否格式化輸出xml
props.put(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
// 輸出編碼
props.put(Marshaller.JAXB_ENCODING, Charsets.UTF_8.name());
// 是否省略xml頭信息(<?xml version="1.0" encoding="gb2312" standalone="yes"?>)
props.put(Marshaller.JAXB_FRAGMENT, false);
return toXml(obj, props);
}
/**
* java對象轉xml
*
* @param obj 對象
* @param props xml轉換的配置熟悉
*
* @return xml
*/
public static String toXml(Object obj, Map<String, Object> props) {
if (obj == null) {
return "";
}
StringWriter sw = new StringWriter();
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
for (Map.Entry<String, Object> entry : props.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
marshaller.setProperty(key, value);
}
marshaller.marshal(obj, sw);
} catch (JAXBException e) {
throw new RuntimeException("convert to Xml failed", e);
}
return sw.toString();
}
/**
* 解析xml
*
* @param xmlStr xml
* @param clazz 類型
*/
public static <T> T parseXml(String xmlStr, Class<T> clazz) {
T xmlObject;
try {
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller unmarshal = context.createUnmarshaller();
StringReader sr = new StringReader(xmlStr);
//noinspection unchecked
xmlObject = (T) unmarshal.unmarshal(sr);
} catch (Exception e) {
throw new RuntimeException("parse from Xml String failed", e);
}
return xmlObject;
}
}
3、Java Bean 轉XMl,實體類
@Data
@ConfigurationProperties(prefix = "wechat.cp")
@RefreshScope
@XmlRootElement(name = "xml")
public class WxCpProperties {
/**
* 設置微信企業號的corpId
*/
@XmlElement(name = "AppId")
private String corpId;
private List<AppConfig> appConfigs;
}
4、轉換異常原因分析:
xception in thread "main" java.lang.RuntimeException: convert to Xml failed at com.tencent.iov.qw.utils.XmlParseUtill.toXml(XmlParseUtill.java:74) at com.tencent.iov.qw.utils.XmlParseUtill.toXml(XmlParseUtill.java:41) at com.tencent.iov.qw.utils.XmlParseUtill.main(XmlParseUtill.java:47) Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions 類的兩個屬性具有相同名稱 "corpId" this problem is related to the following location: at public java.lang.String com.tencent.iov.qw.config.WxCpProperties.getCorpId() at com.tencent.iov.qw.config.WxCpProperties this problem is related to the following location: at private java.lang.String com.tencent.iov.qw.config.WxCpProperties.corpId at com.tencent.iov.qw.config.WxCpProperties
使用@Data注解,但是處理器同時處理訪問了字段熟悉和Get方法,只需要指定處理程序只訪問字段就行了
在類上加上注解: @XmlAccessorType(XmlAccessType.FIELD) ,再次處理正常
5、處理結果
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
<AppId>45166</AppId>
</xml>
