简体   繁体   中英

JAXB multiple @XmlRootElement

I have a class annotated as follows:

@XmlRootElement(name="response")
@XmlType(propOrder={"paymentid", 
                    "result",
                    "responsecode", 
                    "authorizationcode", 
                    "merchantorderid", 
                    "rrn", 
                    "cardcountry", 
                    "cardtype"})
public class MOTOResponseIn {
...
}

The root element of the mapped XML could be also be error beside response .

How can I annotate the class so that both root elements are allowed?

In this case @XmlRootElement can not be used. You have to use ObjectFactory. The @XmlElementDecl annotation is used to represent root elements that correspond to named complex types. It is placed on a factory method in a class annotated with @XmlRegistry (when generated from an XML schema this class is always called ObjectFactory). The factory method returns the domain object wrapped in an instance of JAXBElement Hope this url will help.

https://dzone.com/articles/jaxb-and-root-elements

With a single class and @XmlRootElement it is not possible. However, in case you don't want to mess with ObjectFactory, for a quick workaround you can use abstract and concrete classes. (Assuming the only difference is the root element)

Example:

@XmlAccessorType(XmlAccessType.FIELD)
public abstract class MOTOabstract{
     @XmlAttribute
     private String paymentid;

     @XmlAttribute
     private String result

     @XmlAttribute
     private String responsecode;

     ...
}

@XmlRootElement(name="response")
@XmlAccessorType(XmlAccessType.FIELD)
public class MOTOresponse extends MOTOabstract{}

@XmlRootElement(name="error")
@XmlAccessorType(XmlAccessType.FIELD)
public class MOTOerror extends MOTOabstract{}

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "id",
        "name",
        "serviceAttrs"
    })
    @XmlSeeAlso({ AddGroup.class, AddGroupRequest.class })
    public class AddGroupAbstract {
    
        @XmlElement(required = true)
        protected String id;
        @XmlElement(required = true)
        protected String name;
        @XmlElement(required = true)
        protected ServiceAttrs serviceAttrs;
    
        ...
    }
    
    @XmlRootElement(name = "addGroup")
    public class AddGroup extends AddGroupAbstract {}
    
    @XmlRootElement(name = "addGroupRequest")
    public class AddGroupRequest extends AddGroupAbstract {}
    
@Endpoint
public class GroupEndpoint {
    private final GroupService groupService;
    private final ServiceService serviceService;
    private final RestTemplate restTemplate;

    public GroupEndpoint(GroupService groupService, ServiceService serviceService, RestTemplate restTemplate) {
        this.groupService = groupService;
        this.serviceService = serviceService;
        this.restTemplate = restTemplate;
    }

    @PayloadRoots({
        @PayloadRoot(namespace = SoapConstants.NAMESPACE_ACCOUNT_URI, localPart = "addGroup"),
        @PayloadRoot(namespace = SoapConstants.NAMESPACE_ACCOUNT_URI, localPart = "addGroupRequest")
    })
    @ResponsePayload
    public AddGroupResponse addGroup(@RequestPayload AddGroupAbstract request) {
        AddGroupResponse response = new AddGroupResponse();

        ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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