简体   繁体   English

JAXB与Java继承的Bean对象的绑定

[英]JAXB Binding With Java Inherited Bean Object

i am attempting to bind XML data to below java model bean class using JAXB Api. 我正在尝试使用JAXB Api将XML数据绑定到以下Java模型bean类。 But i am unable to bind it. 但我无法约束它。 Can anyone please give suggestion or coding Using JAXB. 任何人都可以提出建议或使用JAXB进行编码。 i have provided my bean class and input xml. 我提供了我的bean类和输入xml。

Message.java Message.java

@XmlRootElement(name="Message")
class Message{

private Header header = null;
private Body body = null;

@XmlElement(name="Header")
public Header getHeader() {
    return header;
}
public void setHeader(Header header) {
    this.header = header;
}
@XmlElement(name="Body")
public Body getBody() {
    return body;
}
public void setBody(Body body) {
    this.body = body;
}
}

Header.Java 头文件

@XmlSeeAlso({ReqHeader.class})
class Header{

}

ReqHeader.java ReqHeader.java

@XmlRootElement(name="Header")
class ReqHeader extends Header{

private String msgID = null;
private String msgDesc = null;
public String getMsgID() {
    return msgID;
}
public void setMsgID(String msgID) {
    this.msgID = msgID;
}
public String getMsgDesc() {
    return msgDesc;
}
public void setMsgDesc(String msgDesc) {
    this.msgDesc = msgDesc;
}
}

Body.java 正文

class Body{

}

RequestBody.java RequestBody.java

class RequestBody extends Body{
private CustInfo custInfo = null;
public CustInfo getCustrInfo() {
    return custInfo;
}
public void setCustrInfo(CustInfo custInfo) {
    this.custInfo = custInfo;
}
}

CustInfo.java CustInfo.java

class CustInfo{
private String custID = null;
public String getCustID() {
    return custID;
}
public void setCustID(String custID) {
    this.custID = custID;
}
}

MessageUnmarshall .java MessageUnmarshall.java

public class MessageUnmarshall {

public static void main(String a[]) throws JAXBException{
    String str = "<Message> <Header> <MsgID>123</MsgID> <MsgDesc>cust     products</MsgDesc> </Header> <Body> <CustInfo> <CustID>111</CustID> </CustInfo> </Body> </Message>";
    JAXBContext context = JAXBContext.newInstance(Message.class);
    Unmarshaller un = context.createUnmarshaller();
    Message msg = (Message) un.unmarshal(new StringReader(str));
}

}

Alternate way is to not use inheritance and use only header and body class and remove ReqHeader and RequestBody class. 另一种方法是不使用继承,而仅使用标头和正文类,并删除ReqHeader和RequestBody类。

Make the header class like this: 使标题类如下所示:

@XmlRootElement(name = "Header")
class Header {
private String msgID = null;
private String msgDesc = null;

public String getMsgID() {
    return msgID;
}

public void setMsgID(String msgID) {
    this.msgID = msgID;
}

public String getMsgDesc() {
    return msgDesc;
}

public void setMsgDesc(String msgDesc) {
    this.msgDesc = msgDesc;
}

@Override
public String toString() {
    return "ReqHeader [msgID=" + msgID + ", msgDesc=" + msgDesc + "]";
}

}` }`

and change the body class accordingly... 并相应地更改身体类别...

The purpose of the @XmlSeeAlso annotation is just to let your JAXB (JSR-222) implementation know that when it is processing the metadata for Resource that it should also process the metadata for the SomeItem class. @XmlSeeAlso批注的目的仅仅是让您的JAXB(JSR-222)实现知道在处理Resource的元数据时,它也应该处理SomeItem类的元数据。

Some people mistakenly believe that it is related to mapping inheritance since that is the use case it is most often used with. 有些人错误地认为它与映射继承有关,因为这是最经常使用的用例。

Since the subclasses of a class can not be determined using Java reflection, @XmlSeeAlso is used to let the JAXB implementation know that mappings for the subclasses should also be created. 由于无法使用Java反射确定类的子类,因此使用@XmlSeeAlso来使JAXB实现知道也应创建子类的映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM