externally create jaxb annotations for class

后端 未结 2 1680
醉酒成梦
醉酒成梦 2021-02-10 00:58

So, usually I apply JAXB annotations in the code as follows:

package com.example;

@XmlRootElement(name = \"Foo\", namespace = \"example.com\")
@XmlType(name = \         


        
相关标签:
2条回答
  • 2021-02-10 01:37

    Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

    The MOXy implementation of JAXB has an external mapping file that you can use to provide the metadata.

    <?xml version="1.0"?>
    <xml-bindings
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="com.example">
        <java-types>
            <java-type name="Customer">
                <xml-type name="Foo" namespace="example.com"/>
            </java-type>
        </java-types>
    </xml-bindings>
    

    For More Information

    • http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html
    0 讨论(0)
  • 2021-02-10 01:58

    If you just need to add @XmlType(name = "Foo", namespace = "example.com") annotation to the generated class you can use JAXB Annotate Plugin. Here is documentation about how to define annotations in external binding files.

    If you're using CXF and maven you can also you cxf-codegen-plugin somehow like this

    <plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-codegen-plugin</artifactId>
        <version>${cxf.version}</version>
        <executions>
            <execution>
                <id>generate-sources</id>
                <phase>generate-sources</phase>
                <goals>
                    <goal>wsdl2java</goal>
                </goals>
                <configuration>
                    <sourceRoot>${service.src.dir}</sourceRoot>
                    <wsdlOptions>
                        <wsdlOption>
                            <wsdl>service.wsdl</wsdl>
                            <extraargs>
                                <extraarg>-xjc-Xannotate</extraarg>
                                <extraarg>-b</extraarg>
                                <extraarg>${wsdl.dir}/bindings.xjb</extraarg>
                            </extraargs>
                        </wsdlOption>
                    </wsdlOptions>
                </configuration>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics-annotate</artifactId>
                <version>${jaxb.commons.version}</version>
            </dependency>
        </dependencies>                
    </plugin>
    

    You can also use maven-jaxb2-plugin:

    <plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>${maven-jaxb2.version}</version>
    <executions>
        <execution>
            <id>process-xsd</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
                <schemaIncludes>
                    <include>**/*.xsd</include>
                </schemaIncludes>
                <bindingIncludes>
                    <include>**/*.xjb</include>
                </bindingIncludes>
                <generateDirectory>${jaxb.src.dir}</generateDirectory>
                <extension>true</extension>
                <args>
                    <arg>-Xannotate</arg>
                </args>
                <plugins>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics-annotate</artifactId>
                        <version>${jaxb.commons.version}</version>
                    </plugin>
                </plugins>
            </configuration>
        </execution>
    </executions>
    </plugin>
    

    Here is sample binding file:

    <jaxb:bindings
    xmlns:jaxb="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"
    version="2.0">
    
        <jaxb:bindings schemaLocation="schema.xsd" node="/xs:schema">
            <jaxb:bindings node="//xs:complexType[@name='Foo']">
                <annox:annotate target="class">
                    <annox:annotate annox:class="javax.xml.bind.annotation.XmlType" name="Foo" namespace = "example.com"/>
                </annox:annotate>
            </jaxb:bindings>
        </jaxb:bindings>
    
    </jaxb:bindings>
    

    If you need to modify @XmlRootElement too, just add another one annox:annotate element:

    <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="Foo" namespace = "example.com"/>
    
    0 讨论(0)
提交回复
热议问题