如何在 java 对象的 JAXB 中将字段名称大写?

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

我正在尝试使用 JAXB 将 Java 对象转换为 XML,它适用于我,但它将 class 字段的名称转换为小写字母,我不知道怎么改成大写字母。 例如,我的 class:

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

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

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

这是安全性 class:

@Component
public class Security {

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

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

对于 StockQuoteRequest class,Security 字段是大写的,但是对于 Security class,其字段(name, lastName, address) 都印有小的.

我尝试在每个字段上方的安全 class 中添加注释

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

但这根本行不通。 谁能告诉我该怎么做? 谢谢

我在尝试您的代码时在 Java 8 (jdk1.8.0_181) 中遇到以下异常,即当我添加标准 getter/setter 方法时:

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

要修复,我必须做 3 件事中的 一个

  • @XmlElement(name = "Security") 注释向下移动到 getSecurities() 方法

  • @XmlTransient添加到getSecurities()方法

  • @XmlAccessorType(XmlAccessType.NONE)(或FIELD)添加到StockQuoteRequest class

当我执行其中任何一个时,我会得到这样的输出:

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

注意 3 个子元素的顺序是如何错误的。要修复,请将 @XmlType(propOrder = { "name", "lastName", "address" }) 添加到 Security class.

然后,当我将问题中显示的 3 个 @XmlElement 注释添加到 Security class,并应用类似的修复来防止异常时,我得到了我所期望的:

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

完整Minimal, Reproducible Example得到以上输出:

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 + "]";
    }
}