更改XJC生成的类的方法名称

时间:2018-07-31 15:48:43

标签: java jaxb xjc

我正在尝试编写一个监视系统,该监视系统允许提供外部系统状态的状态...通过ping服务器,查看日志元素,查询数据库,访问Web服务等。由于每个应用程序都有独特的行为,因此监视系统需要保持灵活性,以使监视器最适合这些行为。

这是XSD的一部分,用于创建“ Test”类,该类允许用户构建监视器:

<xs:element name="Test">
    <xs:complexType>
        <xs:sequence maxOccurs="unbounded" >
            <xs:element ref="Ping"/>
            <xs:element ref="CheckWebService"/>
            <xs:element ref="CheckDB"/>
            <xs:element ref="ExecuteScript"/>
            <xs:element ref="CheckJMS"/>
            <xs:element ref="CheckLog" />
        </xs:sequence>
        <xs:attribute name="testTitle"/>
    </xs:complexType>
</xs:element>

通过XJC(通过Maven JAXB插件)运行会产生:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "pingsAndCheckLogsAndCheckJMs"
})
@XmlRootElement(name = "Test")
public class Test
    implements Serializable
{

    private final static long serialVersionUID = 1L;
    @XmlElements({
        @XmlElement(name = "Ping", required = true, type = Ping.class),
        @XmlElement(name = "CheckLog", required = true, type = CheckLog.class),
        @XmlElement(name = "CheckJMS", required = true, type = CheckJMS.class),
        @XmlElement(name = "ExecuteScript", required = true, type = ExecuteScript.class),
        @XmlElement(name = "CheckDB", required = true, type = CheckDB.class),
        @XmlElement(name = "CheckWebService", required = true, type = CheckWebService.class)
    })
    protected List<Serializable> pingsAndCheckLogsAndCheckJMs;
    @XmlAttribute(name = "testTitle")
    @XmlSchemaType(name = "anySimpleType")
    protected String testTitle;

    public List<Serializable> getPingsAndCheckLogsAndCheckJMs() {
        if (pingsAndCheckLogsAndCheckJMs == null) {
            pingsAndCheckLogsAndCheckJMs = new ArrayList<Serializable>();
        }
        return this.pingsAndCheckLogsAndCheckJMs;
    }

    public String getTestTitle() {
        return testTitle;
    }

    public void setTestTitle(String value) {
        this.testTitle = value;
    }
}

我的问题是,我该如何重命名 pingsAndCheckLogsAndCheckJMs 方法,因为每次我添加新的测试类型(Ping / CherckDB / CheckLog ...)时,此方法名称都会更改,并且编组对象时的XML标记也很丑陋。

1 个答案:

答案 0 :(得分:0)

您需要指定绑定以显式命名属性。

这是内联的示例。有关更多信息,请参见SYSTEM_ALERT_WINDOW

摘要:您需要向<xs:schema>元素添加2个属性:

<xs:schema ...
           xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
           jxb:version="1.0">

以及<xs:sequence>元素中的以下内容,当然会将该属性命名为您认为合适的名称:

<xs:sequence maxOccurs="unbounded">
    <xs:annotation>
        <xs:appinfo>
            <jxb:property name="foo"/>
        </xs:appinfo>
    </xs:annotation>
    ...

以下完整的Customizing JAXB Bindings(MCVE):

XSD文件

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema version="1.0" targetNamespace="http://example.com/test"
           xmlns="http://example.com/test"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
           jxb:version="1.0">
    <xs:element name="Test">
        <xs:complexType>
            <xs:sequence maxOccurs="unbounded">
                <xs:annotation>
                    <xs:appinfo>
                        <jxb:property name="foo"/>
                    </xs:appinfo>
                </xs:annotation>
                <xs:element ref="Ping"/>
                <xs:element ref="CheckLog"/>
            </xs:sequence>
            <xs:attribute name="testTitle"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="Ping">
        <xs:complexType>
            <xs:sequence>
            </xs:sequence>
            <xs:attribute name="action" type="xs:string"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="CheckLog">
        <xs:complexType>
            <xs:sequence>
            </xs:sequence>
            <xs:attribute name="action" type="xs:string"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

测试

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import com.example.test.CheckLog;
import com.example.test.Ping;
import com.example.test.Test;

public class Test8 {
    public static void main(String[] args) throws Exception {
        Test root = new Test();
        root.setTestTitle("My title");
        root.getFoo().add(newPing("ping 1"));
        root.getFoo().add(newCheckLog("check log 1"));
        root.getFoo().add(newPing("ping 2"));

        JAXBContext jaxbContext = JAXBContext.newInstance(Test.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(root, System.out);
    }
    private static Ping newPing(String action) {
        Ping obj = new Ping();
        obj.setAction(action);
        return obj;
    }
    private static CheckLog newCheckLog(String action) {
        CheckLog obj = new CheckLog();
        obj.setAction(action);
        return obj;
    }
}

输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Test xmlns="http://example.com/test" testTitle="My title">
    <Ping action="ping 1"/>
    <CheckLog action="check log 1"/>
    <Ping action="ping 2"/>
</Test>

如您所见,该方法现在命名为getFoo(),并且所生成的XML中没有难看的东西。

以上操作是使用jdk1.8.0_151完成的。

相关问题