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

1、POM 依赖  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<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、转换工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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,实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Data
@ConfigurationProperties(prefix = "wechat.cp")
@RefreshScope
@XmlRootElement(name = "xml")
public class WxCpProperties {
  /**
   * 设置微信企业号的corpId
   */
  @XmlElement(name = "AppId")
  private String corpId;
 
  private List<AppConfig> appConfigs;
 
}

  4、转换异常原因分析:  

1
2
3
4
5
6
7
8
9
10
11
12
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、处理结果 

1
2
3
4
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
    <AppId>45166</AppId>
</xml>

 

  

 

posted on   滚动的蛋  阅读(2036)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示