RuaTre
RuaTre

Reputation: 111

Marshal object using JAXB incorrect Tag name

I have Root object ProcessConsignmentRequest

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "ProcessConsignmentRequest", propOrder = {
    "request"
    })
    @XmlRootElement(name = "ProcessConsignmentRequest", namespace ="http://tempuri.org/")
public class ProcessConsignmentRequest {{
@XmlElementRef(name = "Request", namespace = "http://tempuri.org/", type = JAXBElement.class, required = false)
protected JAXBElement<ConsignmentRequest> request;

When I Marshal this object. The object'tag of ConsignmentRequest default set to "ConsignmentRequest" but I want it exactly is "Request". Some suggestion for me. Thanks . This xml result: enter image description here

Upvotes: 0

Views: 554

Answers (1)

Alexander
Alexander

Reputation: 11

Had the same issue. As I discovered, JAXBElement doesn't look to the name in your @XmlElementRef annotation. It looks to the QName that you pass to it's constructor. In my case it was

new JAXBElement<>(someService.getServiceName(), Some.class, someInstance)

I changed that code to the line below and got what I wanted:

new JAXBElement<>(new QName("MyElementName"), Some.class, someInstance)

Upvotes: 1

Related Questions