java更改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;
}
}
我的问题是,既然每次我添加一个新的测试类型(Ping/CherckDB/CheckLog…),我如何重命名pingsAndCheckLogsAndCheckJMs方法此方法名称会更改,封送对象时XML标记也会更改
# 1 楼答案
您需要指定一个绑定来显式命名属性
下面是如何内联完成的示例。有关更多信息,请参见Customizing JAXB Bindings
小结:您需要向
<xs:schema>
元素添加两个属性:下面是
<xs:sequence>
元素中的内容,当然,您可以用您认为合适的方式命名属性:全文如下Minimal, Complete, and Verifiable example(MCVE):
XSD文件
测试
输出
如您所见,该方法现在被命名为
getFoo()
,并且生成的XML中没有任何丑陋之处以上是使用jdk1完成的。8.0_151.