简体   繁体   English

JAXB模式到Java不同的XmlRootElement名称和类名

[英]JAXB schema to Java Different XmlRootElement name and Class name

I have a xsd schema from which I'm generating some java classes. 我有一个xsd架构,我正在从中生成一些java类。 I'm using jaxb for the generation. 我正在使用jaxb代代相传。

I want to be able to generate a class annotated with @XmlRootElement , but I want the @XmlRootElement name property to be different than the name of the generated class. 我希望能够生成一个使用@XmlRootElement注释的类,但我希望@XmlRootElement name属性与生成的类的名称不同。

In my xsd I'm defining the following: 在我的xsd中,我定义了以下内容:

<xs:element name="customer">
    <xs:complexType>
        <xs:sequence>
         ....
        </xs:sequence>
     </xs:complexType>
</xs:element>

This piece of code generates the following java class: 这段代码生成以下java类:

@XmlRootElement(name = "customer")
public class Customer {
...
}

The name property of the @XmlRootElement is the same as the name of the generated Class. @XmlRootElement的name属性与生成的Class的名称相同。 I want the generated class name to be CustomerReques t. 我希望生成的类名是CustomerReques t。

I've tryed to use the jaxb:class definition to change the classe name. 我已经尝试使用jaxb:class定义来更改classe名称。 Indeed this option changes the class name but removes the @XmlRootElement annotation, and I need it to be present. 实际上,此选项会更改类名,但会删除@XmlRootElement注释,我需要它存在。

The following xsd: 以下xsd:

<xs:element name="customer">
    <xs:complexType>
        <xs:annotation>
                <xs:appinfo>
                    <jaxb:class name="CustomerRequest"/>
                </xs:appinfo>
            </xs:annotation>
        <xs:sequence>
        </xs:sequence>
    </xs:complexType>
</xs:element>

Generates this class: 生成这个类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "customer", propOrder = {

})
public class CustomerRequest {
}

How can I make the property name of the @XmlRootElement annotation different from the generated class name without loosing the annotation? 如何在不丢失注释的情况下使@XmlRootElement注释的属性名与生成的类名不同?

Solution update: User Xstian proposed the correct solution using external bindings. 解决方案更新:用户Xstian使用外部绑定提出了正确的解决方案。 Just for further reference, I'll update my own post with the solution converted for using inline bindings: 仅供进一步参考,我将使用内联绑定转换的解决方案更新我自己的帖子:

 <xs:element name="customer">
        <xs:complexType>
            <xs:annotation>
                <xs:documentation>Request object for the operation that checks if a customer profile exists.</xs:documentation>
                <xs:appinfo>
                        <annox:annotate>
                            <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="customer"/>
                        </annox:annotate>
                        <jaxb:class name="CustomerRequest"/>
                    </xs:appinfo>
                </xs:annotation>
            <xs:sequence>
            </xs:sequece>
    </xs:complexType>
</xs:element>

I suggest you to use this bindings 我建议你使用这个绑定

<bindings version="2.0" xmlns="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:annox="http://annox.dev.java.net"
    xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix">
    <bindings schemaLocation="../your.xsd">

        <bindings node="//xs:element[@name='customer']//xs:complexType">
            <annox:annotate>
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement"
                    name="customer" namespace="yourNamespaceIfYouWant">
                </annox:annotate>
            </annox:annotate>
            <class name="CustomerRequest"/>
        </bindings>

    </bindings>
</bindings>

Class

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "header"
})
@XmlRootElement(name = "customer", namespace = "yourNamespaceIfYouWant")
public class CustomerRequest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有相同名称的JAXB @XmlRootElement - JAXB @XmlRootElement with the same name 使用超类 XMLRootElement 名称值的 JAXB 注释子类 - JAXB Annotated Child Class Using Super Classes XMLRootElement Name Value jaxb版本2.0不使用xmlrootelement中的name属性 - jaxb version 2.0 not using the name attribute in xmlrootelement 如何在JAXB中更改对象的XmlRootElement名称? - How to change the name of the XmlRootElement in JAXB deppending on the object? JAXB java class generation from schema: how to get a custom XML element name (keep class name) - JAXB java class generation from schema: how to get a custom XML element name (keep class name) JAXB从架构不生成@XmlRootElement - JAXB generates no @XmlRootElement from schema JAX-RS JAXB Jackson 不使用@XmlRootElement 名称 - JAX-RS JAXB Jackson not using @XmlRootElement name 将Jaxb2Marshaller与具有相同@XmlRootElement名称的多个类一起使用 - Using Jaxb2Marshaller with multiple classes having same @XmlRootElement name 在任何情况下,JAXB中@XmlRootElement的注释是否必须添加Java类? - Does the annotation of @XmlRootElement in JAXB have to add Java class in any case? Java / JAXB:将具有相同名称但不同属性值的XML元素解组为不同的类成员 - Java/JAXB: Unmarshall XML elements with same name but different attribute values to different class members
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM