架构中的Jaxb类以大写形式生成getter setter方法 [英] Jaxb classes from schema generates getter setter method in uppercase

查看:287
本文介绍了架构中的Jaxb类以大写形式生成getter setter方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAX-WS生成自上而下的Web服务。在wsdl中我导入了一个包含所有必要元素的xsd。我需要在soap请求中以大写形式显示所有xml节点,因此我将xsd中的元素名称保留为大写。但是在从wsdl生成类时,我能够看到getter和setter方法也以大写形式命名,但不是变量。例如,如果我使用'SOURCE'作为xsd中的元素,我将相应的getter方法作为'getSOURCE'而不是普通约定'getSource'。我尝试通过从模式生成jaxb类来保持xsd,但是我无法获得预期的命名约定。

I'm generating a top down web service with JAX-WS. In the wsdl I'm importing a xsd containing all necessary elements. I need all the xml nodes to be displayed in uppercase in soap request and hence I kept the element names in the xsd to upper case. But while generating the classes from the wsdl, I am able to see the getter and setter method are named in upper case as well but not the variables. For ex., if I use 'SOURCE' as an element in xsd, i'm getting the corresponding getter method as 'getSOURCE' instead of the normal convention 'getSource'. I tried to play by generating the jaxb classes from schema with keeping the xsd alone but i was not able to get the expected naming convention.

下面是使用模式的一部分

Below is a part of schema used

<xs:complexType name="Data">
<xs:sequence>
  <xs:element name="SEQ" type="xs:int"/>
  <xs:element name="INDEX" type="xs:string"/>
  <xs:element name="VALUE" type="xs:string"/>
</xs:sequence>

这产生以下代码jaxb绑定 -

This produces the following code after jaxb binding -

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Data", propOrder = {
    "seq",
    "index",
    "value"
})
public class Data {

@XmlElement(name = "SEQ")
protected int seq;
@XmlElement(name = "INDEX", required = true)
protected String index;
@XmlElement(name = "VALUE", required = true)
protected String value;

public int getSEQ() {
    return seq;
}

public void setSEQ(int value) {
    this.seq = value;
}

public String getINDEX() {
    return index;
}

public void setINDEX(String value) {
    this.index = value;
}

public String getVALUE() {
    return value;
}

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

}

这里的setter方法生成为setSEQ和我需要在像'setSeq'这样的驼峰情况下。

Here setter method is getting generated as setSEQ and I need that to be in camel case like 'setSeq'.

推荐答案

这是预期的行为。底层 xjc 调用遵循Java Bean约定:如果属性名称的前两个字母是大写,则不对get和set方法应用大小写或decapitalization。

This is expected behavior. The underlying xjc invocation is observing Java Bean conventions: If the first two letters of a property name are uppercase, no capitalization or decapitalization is applied to the get and set methods.

您可以在JAXB外部绑定文件中指定自己的重写方法绑定,该文件是一个XML文件,按照惯例,它具有 .xjb 扩展名。它的格式在 JAXB规范的第7章中描述:

You can specify your own overriding method bindings in an JAXB external bindings file, which is an XML file that by convention has an .xjb extension. Its format is described in chapter 7 of the JAXB specification:

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.0"
               xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <jaxb:bindings schemaLocation="data.xsd">

        <jaxb:bindings node="//xs:complexType[@name='Data']">
            <jaxb:bindings node=".//xs:element[@name='SEQ']">
                <jaxb:property name="seq"/>
            </jaxb:bindings>
            <jaxb:bindings node=".//xs:element[@name='INDEX']">
                <jaxb:property name="index"/>
            </jaxb:bindings>
            <jaxb:bindings node=".//xs:element[@name='VALUE']">
                <jaxb:property name="value"/>
            </jaxb:bindings>
        </jaxb:bindings>

    </jaxb:bindings>

</jaxb:bindings>

请注意data.xsd应该替换为模式文件的相对URI。

Note that "data.xsd" should be replaced with the relative URI of your schema file.

您可以将上述内容放在名为 custom.xjb ,然后将其传递给你的wsimport调用:

You would place the above in a file named something like custom.xjb, then pass it to your wsimport invocation:

wsimport -d build/generated-classes -p com.example.myapp.data -b custom.xjb http://www.example.com/data-service.wsdl

这篇关于架构中的Jaxb类以大写形式生成getter setter方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆