本文介绍了您如何自定义 JAXB 生成复数方法名称的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用JAXB生成Java类,遇到过几个生成的复数方法名不正确的情况.例如,在我们期望 getPhysicians 的地方,我们得到了 getPhysicien.我们将如何自定义 JAXB 如何将特定方法复数化?

We are using JAXB to generate Java classes and have encountered a few cases where generated plural method names are not correct. For example, where we expect getPhysicians we are getting getPhysicien. How would we customize how JAXB pluralizes specific methods?

架构:

<xs:complexType name="physician">
    <xs:sequence>
       ...
    </xs:sequence>
</xs:complexType>

<xs:complexType name="physicianList">
    <xs:sequence>
        <xs:element name="Physician"
                    type="physician"
                    minOccurs="0"
                    maxOccurs="unbounded"/>
    </xs:sequence>
</xs:complexType>

生成的Java代码:

...
public class PhysicianList {
...

    @XmlElement(name = "Physician")
    protected List<Physician> physicien;
    ...

    public List<Physician> getPhysicien() {
        if (physicien == null) {
            physicien = new ArrayList<Physician>();
        }
        return this.physicien;
    }

更新

布莱斯已经回答了这个问题.但是,我不喜欢在 XML 模式中混合诸如 JAXB 定制之类的问题.因此,对于那些有相同偏好的人,这里有一个 JAXB 绑定文件,它实现了与 Blaise 建议的相同的事情,将 JAXB 定制排除在架构之外:

This has been answered by Blaise. However, I prefer not mixing concerns such as JAXB customizations in an XML schema. So for those of you with the same preference, here is a JAXB binding file that achieves the same thing as what Blaise suggested, keeping JAXB customization out of the schema:

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

    <jaxb:bindings schemaLocation="myschema.xsd">
        <jaxb:bindings node="//xs:complexType[@name='physicianList']//xs:element[@name='Physician']">
            <jaxb:property name="physicians"/>
        </jaxb:bindings>
    </jaxb:bindings>

</jaxb:bindings>

推荐答案

默认情况下,为您的架构片段生成以下内容:

By default the following is generated for your schema fragment:

    import java.util.ArrayList;
    import java.util.List;
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlType;

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "physicianList", propOrder = {
        "physician"
    })
    public class PhysicianList {

        @XmlElement(name = "Physician")
        protected List<Physician> physician;

        public List<Physician> getPhysician() {
            if (physician == null) {
                physician = new ArrayList<Physician>();
            }
            return this.physician;
        }

    }

如果您注释 XML 架构:

If you annotate your XML schema:

    <xs:schema
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        jaxb:version="2.1">

        <xs:complexType name="physician">
            <xs:sequence>
            </xs:sequence>
        </xs:complexType>

        <xs:complexType name="physicianList">
            <xs:sequence>
                <xs:element name="Physician"
                            type="physician"
                            minOccurs="0"
                            maxOccurs="unbounded">
                      <xs:annotation>
                          <xs:appinfo>
                              <jaxb:property name="physicians"/>
                          </xs:appinfo>
                      </xs:annotation>
                 </xs:element>
            </xs:sequence>
        </xs:complexType>

    </xs:schema>

然后就可以生成想要的类了:

Then you can generate the desired class:

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "physicianList", propOrder = {
    "physicians"
})
public class PhysicianList {

    @XmlElement(name = "Physician")
    protected List<Physician> physicians;

    public List<Physician> getPhysicians() {
        if (physicians == null) {
            physicians = new ArrayList<Physician>();
        }
        return this.physicians;
    }

}

这篇关于您如何自定义 JAXB 生成复数方法名称的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 14:19