Web Services 在外部为类创建JAXB注解

lmyy7pcs  于 2023-06-06  发布在  其他
关注(0)|答案(2)|浏览(178)

因此,我通常在代码中应用JAXB注解,如下所示:

package com.example;

@XmlRootElement(name = "Foo", namespace = "example.com")
@XmlType(name = "Foo", namespace = "example.com")
public class Foo {
    ...
}

Foo是一个Java类,用于与Web服务通信(通过Spring/CXF)。上面的注解有助于在wsdl中正确地生成XML模式。
我遇到过这样的情况:我不能修改类本身,但是我可以向生成模式的代码提供一个JAXB外部绑定文件。请注意,类中存在@XmlRootElement。
我如何编写一个等效的绑定文件来完成上面的注解所做的事情?

x7rlezfr

x7rlezfr1#

如果您只需要将@XmlType(name = "Foo", namespace = "example.com")注解添加到生成的类中,您可以使用JAXB注解插件。
如果你正在使用CXF和maven,你也可以像这样使用cxf-codegen-plugin

<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>

你也可以使用maven-jaxb 2-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>

下面是示例绑定文件:

<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>

如果你也需要修改@XmlRootElement,只需添加一个annox:annotate元素:

<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="Foo" namespace = "example.com"/>
z2acfund

z2acfund2#

**注意:**我是EclipseLink JAXB (MOXy)的负责人,也是JAXB (JSR-222)Maven组的成员。

JAXB的MOXy实现有一个外部Map文件,您可以使用它来提供元数据。

<?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>

更多信息

相关问题