【Bean转XML】JAXB读取xml文档转换为java对象报错:IllegalAnnotationExceptions 类的两个属性具有相同名称 "corpId"
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>