外部为类创建 jaxb 注解

发布于 2024-12-15 22:17:54 字数 406 浏览 2 评论 0原文

因此,通常我在代码中应用 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。

如何编写一个等效的绑定文件来完成上述注释的功能?

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

package com.example;

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

Foo is a java class that is used to communicate with web services (via Spring/CXF). The above annotations, help generate the XML Schema in the wsdl appropriately.

I have hit a situation where I can not modify the class itself, but I can provide an jaxb external binding file to the code that generates the schema. Note that the @XmlRootElement exists in the class.

How do I write an equivalent binding file that does what the above annotations do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愿得七秒忆 2024-12-22 22:17:54

如果您只需要向生成的类添加 @XmlType(name = "Foo", namespace = "example.com") 注释,您可以使用 JAXB Annotate Plugin。

如果您使用 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-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>

这是示例绑定文件:

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

If you just need to add @XmlType(name = "Foo", namespace = "example.com") annotation to the generated class you can use JAXB Annotate Plugin.

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"/>
一场春暖 2024-12-22 22:17:54

注意:我是EclipseLink JAXB (MOXy) 的领导者和 JAXB 的成员(JSR-222)专家组。

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

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

了解更多信息

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文