How to do fields names uppercase in JAXB in java object?

Kirill Sereda :

I am trying to convert a Java Object to XML using JAXB and it works for me, but it converts the name of the class fields with a lowercase letter and I don’t know how to change to capital letters. For example, my class:

@XmlRootElement(name = "StockQuoteRequest")
@Component
public class StockQuoteRequest {

    @XmlElement(name = "Security")
    private Security securities;

// constructor   
// getter/setter
// toString
}

Here is the Security class:

@Component
public class Security {

    private String name;
    private String lastName;
    private String address;

    // constructor   
    // getter/setter
    // toString
}

For the StockQuoteRequest class, the Security field is capitalized, but for the Security class, its fields (name, lastName, address) are printed with a small one.

I tried to add an annotation in the Security class above each field

@XmlElement(name = "Name")
@XmlElement(name = "LastName")
@XmlElement(name = "Address")

But this does not work at all. Can anyone tell me how to do this? Thanks.

Andreas :

I get the following exception in Java 8 (jdk1.8.0_181) when trying your code, i.e. when I added standard getter/setter methods:

Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Class has two properties of the same name "securities"
    this problem is related to the following location:
        at public Security StockQuoteRequest.getSecurities()
        at StockQuoteRequest
    this problem is related to the following location:
        at private Security StockQuoteRequest.securities
        at StockQuoteRequest

To fix, I had to do one of 3 things:

  • Move the @XmlElement(name = "Security") annotation down to the getSecurities() method

  • Add @XmlTransient to the getSecurities() method

  • Add @XmlAccessorType(XmlAccessType.NONE) (or FIELD) to the StockQuoteRequest class

When I did any of those, I would get output like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<StockQuoteRequest>
    <Security>
        <address>North Pole</address>
        <lastName>Doe</lastName>
        <name>John</name>
    </Security>
</StockQuoteRequest>

Notice how the 3 sub-elements are in the wrong order. To fix, add @XmlType(propOrder = { "name", "lastName", "address" }) to the Security class.

When I then added the 3 @XmlElement annotations, showed in the question, to the Security class, and applied similar fix to prevent exception, I get what I expected:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<StockQuoteRequest>
    <Security>
        <Name>John</Name>
        <LastName>Doe</LastName>
        <Address>North Pole</Address>
    </Security>
</StockQuoteRequest>

Full Minimal, Reproducible Example to get the above output:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

public class Test {
    public static void main(String[] args) throws Exception {
        StockQuoteRequest request = new StockQuoteRequest(
                new Security("John", "Doe", "North Pole"));

        JAXBContext jaxbContext = JAXBContext.newInstance(StockQuoteRequest.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(request, System.out);
    }
}

@XmlRootElement(name = "StockQuoteRequest")
@XmlAccessorType(XmlAccessType.NONE)
class StockQuoteRequest {

    @XmlElement(name = "Security")
    private Security securities;

    public StockQuoteRequest() {
    }

    public StockQuoteRequest(Security securities) {
        this.securities = securities;
    }

    public Security getSecurities() {
        return this.securities;
    }

    public void setSecurities(Security securities) {
        this.securities = securities;
    }

    @Override
    public String toString() {
        return "StockQuoteRequest" + this.securities;
    }

}

@XmlType(propOrder = { "name", "lastName", "address" })
@XmlAccessorType(XmlAccessType.NONE)
class Security {

    @XmlElement(name = "Name")
    private String name;
    @XmlElement(name = "LastName")
    private String lastName;
    @XmlElement(name = "Address")
    private String address;

    public Security() {
    }

    public Security(String name, String lastName, String address) {
        this.name = name;
        this.lastName = lastName;
        this.address = address;
    }

    public String getName() {
        return this.name;
    }

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

    public String getLastName() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Security[name=" + this.name + ", lastName=" + this.lastName + ", address=" + this.address + "]";
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=385043&siteId=1