创建外部类JAXB批注 [英] externally create jaxb annotations for class

查看:147
本文介绍了创建外部类JAXB批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,平时我申请在code JAXB注解如下:

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是用于与Web服务通信的java类(通过Spring / CXF)。上述注解,帮助生成WSDL中适当的XML模式。

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.

我击中的情况下,我不能修改类本身,但我可以提供一个JAXB外部绑定文件到code产生的模式。注意,@XmlRootElement存在于类

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?

推荐答案

如果你只需要添加 @XmlType(NAME =富,命名空间=example.com)注释生成的类可以使用 JAXB注释插件
<一href=\"http://confluence.highsource.org/display/J2B/Annotate+Plugin#AnnotatePlugin-Definingannotationsinexternalbindingfiles\"相对=nofollow>这里是有关如何在外部约束力的文件中定义注释文档。

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.

如果您正在使用CXF和Maven你也可以你cxf- codeGEN-插件莫名其妙地这样

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>

您也可以使用Maven的JAXB2-插件:

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>

下面是示例绑定文件:

<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:注释元素:

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

这篇关于创建外部类JAXB批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆