使用@XmlSeeAlso注解,xml转换bean时,转换的对象类型总是为同一个
程序员文章站
2022-06-02 15:41:44
...
@XmlSeeAlso({AccountInfoQueryRequest.class, AccountInfoRequest.class})
public abstract class Request {
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "request")
@XmlType(name = "AccountInfoRequest")
public class AccountInfoRequest extends Request {
private A a;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "response")
@XmlType(name = "AccountInfoResponse")
public class AccountInfoResponse extends Response {
private B b;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "body")
public class Body {
@XmlElementRef
private Request request;
}
如上,xml转换成Body对象时,不管xml的request标签内容是什么,总是转换成AccountInfoRequest对象,
解决方法:使用@XmlElementRefs,@XmlElementRef注解映射对应的对象类型,对象转换符合预期
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "body")
public class Body {
@XmlElementRefs({
@XmlElementRef(name = "AccountInfoRequest", type = AccountInfoRequest.class),
@XmlElementRef(name = "AccountInfoQueryRequest", type = AccountInfoQueryRequest.class)
})
private Request request;
}
参考
https://bbs.csdn.net/topics/392427716