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

Kir*_*eda 1 java spring jaxb spring-boot

我正在尝试使用 JAXB 将 Java 对象转换为 XML,它对我有用,但它会将类字段的名称转换为小写字母,而我不\xe2\x80\x99t 知道如何更改为大写字母。\nFor例如,我的班级:

\n\n
@XmlRootElement(name = "StockQuoteRequest")\n@Component\npublic class StockQuoteRequest {\n\n    @XmlElement(name = "Security")\n    private Security securities;\n\n// constructor   \n// getter/setter\n// toString\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是安全类:

\n\n
@Component\npublic class Security {\n\n    private String name;\n    private String lastName;\n    private String address;\n\n    // constructor   \n    // getter/setter\n    // toString\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

对于StockQuoteRequest类,Security 字段是大写的,但对于 Security 类,其字段(name、lastName、address)打印为小写。

\n\n

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

\n\n
@XmlElement(name = "Name")\n@XmlElement(name = "LastName")\n@XmlElement(name = "Address")\n
Run Code Online (Sandbox Code Playgroud)\n\n

但这根本不起作用。\n谁能告诉我该怎么做?\n谢谢。

\n

And*_*eas 5

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

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
Run Code Online (Sandbox Code Playgroud)

为了解决这个问题,我必须做以下三件事之一:

  • 将注释移至方法@XmlElement(name = "Security")getSecurities()

  • 添加@XmlTransientgetSecurities()方法中

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

当我执行其中任何一个操作时,我会得到如下输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<StockQuoteRequest>
    <Security>
        <address>North Pole</address>
        <lastName>Doe</lastName>
        <name>John</name>
    </Security>
</StockQuoteRequest>
Run Code Online (Sandbox Code Playgroud)

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

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

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<StockQuoteRequest>
    <Security>
        <Name>John</Name>
        <LastName>Doe</LastName>
        <Address>North Pole</Address>
    </Security>
</StockQuoteRequest>
Run Code Online (Sandbox Code Playgroud)

获得上述输出的完整最小、可重现示例:

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 + "]";
    }
}
Run Code Online (Sandbox Code Playgroud)