超超超zzzz 2021-10-31 18:41 采纳率: 0%
浏览 84

xml转bean对象问题(带泛型)

最近工作时遇到一个问题:需要将收到的报文转换为对应的对象,由于报文头(head标签)中的内容结构固定,而报文体(body标签)中的内容结构根据业务不同会发生变化,因此打算报文体使用泛型来接收,这个过程中发现泛型总是不起作用,测试代码如下:

/**
 * 通用报文接收对象
 * @param <T>
 */
@XmlRootElement(name = "service")
@XmlSeeAlso(testReqBody.class)
public abstract class BaseReqDto<T> {


    private BaseReqHead reqHead;

    @XmlAnyElement(lax = true)
    private T reqBody;

    @XmlElement(name = "head")
    public BaseReqHead getReqHead() {
        return reqHead;
    }

    public void setReqHead(BaseReqHead reqHead) {
        this.reqHead = reqHead;
    }

    @XmlTransient
    public T getReqBody() {
        return reqBody;
    }

    public void setReqBody(T reqBody) {
        this.reqBody = reqBody;
    }

    @Override
    public String toString() {
        return "BaseReqDto{" +
                "reqHead=" + reqHead +
                ", reqBody=" + reqBody +
                '}';
    }
}
/**
 * 报文头对象
 */
@XmlRootElement(name = "head")
@XmlAccessorType(XmlAccessType.FIELD)
public class BaseReqHead {


    @XmlElement(name = "name")
    private String name;

    @XmlElement(name = "age")
    private String age;

    @XmlElement(name = "sex")
    private String sex;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }


    @Override
    public String toString() {
        return "ReqHead{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

/**
 * 测试用报文体
 */
@XmlRootElement(name = "body")
@XmlAccessorType(XmlAccessType.FIELD)
public class testReqBody {

    @XmlElement(name = "chinese")
    public String chinese;

    @XmlElement(name = "math")
    public String math;

    public String getChinese() {
        return chinese;
    }

    public void setChinese(String chinese) {
        this.chinese = chinese;
    }

    public String getMath() {
        return math;
    }

    public void setMath(String math) {
        this.math = math;
    }

    @Override
    public String toString() {
        return "testReqBody{" +
                "chinese='" + chinese + '\'' +
                ", math='" + math + '\'' +
                '}';
    }
}
<?xml version="1.0" encoding="utf-8"?>
<service>
    <head>
        <name>lc</name>
        <age>24</age>
        <sex></sex>
    </head>
    <body>
        <chinese>100</chinese>
        <math>135</math>
    </body>
</service>

报错信息:

Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Could not unmarshal to [class com.example.oracle_demo.entity.BaseReqDto]: Unable to create an instance of com.example.oracle_demo.entity.BaseReqDto; nested exception is javax.xml.bind.UnmarshalException: Unable to create an instance of com.example.oracle_demo.entity.BaseReqDto
 - with linked exception:
[java.lang.InstantiationException]]
  • 写回答

1条回答 默认 最新

  • zcl_1991 2021-11-01 09:45
    关注

    怎么用的不截出来

    评论

报告相同问题?

问题事件

  • 创建了问题 10月31日

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳