注释CXF(wsdl2java)生成的包

问题描述 投票:1回答:4

我需要添加包级别注释(XmlJavaTypeAdapters类型适配器)。问题是,当我运行wsdl2java时,它会为该包生成package-info.java文件。

当我尝试添加自己的package-info.java时,我收到错误:“类型package-ingo已经定义”。

有没有办法将我的注释注入package-info.java?也许还有其他想法吗?

谢谢

java jaxb cxf wsdl2java
4个回答
4
投票

经过一番研究,我使用了外部映射文件。对于所有与我有类似问题的人,我在下面描述了我发现的内容。

如果您使用“cxf-codegen-plugin”从WSDL生成源代码,则无法将解决方案与package-info.java一起使用。这是因为生成的代码可能已经包含此文件。您也不能在类中添加注释,因为它是生成的。唯一的解决方案是提供自己的映射器。

首先,你必须编写自定义映射器。之后,您应该定义xjb映射文件,最后将其他配置添加到您的pom.xml中。你可以阅读前两个步骤here

要将外部映射文件添加到cxf-codegen-plugin,您必须在插件定义中向配置节点添加类似这样的内容:

<defaultOptions>
    <bindingFiles>
        <bindingFile>${basedir}/src/main/resources/mapping.xjb</bindingFile>
    </bindingFiles>
    <noAddressBinding>true</noAddressBinding>
</defaultOptions>

请注意,您不应该像here所描述的那样将额外的参数传递给xjc,因为它不起作用。

希望这会对任何人有所帮助:)


2
投票

我从未尝试过这个,但您可以尝试在wsdl2java命令中添加-xjc-npa标志。从理论上讲,它告诉XJC不要生成一个package-info.java,而是将所有名称空间等都粘贴到需要它的所有其他元素上。


0
投票

您可以在WSDL中内联或作为单独的外部绑定文件提供JAXB“绑定”,JAXB将生成适当的适配器和所需的包级注释。有关示例,请参阅this question


0
投票

我还需要为生成的代码添加注释。我使用maven-replacer-plugin在生成java类之后执行此操作。您可以使用此解决方案来修改任何出来的文件。

这是相关的pom.xml位:

        <plugin>
            <groupId>com.google.code.maven-replacer-plugin</groupId>
            <artifactId>replacer</artifactId>
            <version>${replacer.plugin.version}</version>
            <executions>
                <execution>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>replace</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <filesToInclude>target/generated-sources/cxf/com/BLAH/client/api/v4/*.java</filesToInclude>
                <filesToExclude>target/generated-sources/cxf/com/BLAH/client/api/v4/ObjectFactory.java,
                    target/generated-sources/cxf/com/BLAH/client/api/v4/package-info.java,
                </filesToExclude>
                <replacements>
                    <replacement>
                        <!-- Add @XmlRootElement in front of public class Blah -->
                        <token>public class (\w*)</token>
                        <value>@XmlRootElement(name ="$1") ${line.separator}public class $1</value>
                    </replacement>
                    <replacement>
                        <!-- Add the import for the XmlRootElement annotation to the file -->
                        <token>import javax.xml.bind.annotation.XmlType;</token>
                        <value>import javax.xml.bind.annotation.XmlType;${line.separator}import javax.xml.bind.annotation.XmlRootElement;</value>
                    </replacement>
                </replacements>
            </configuration>
        </plugin>

希望这可以帮助!

© www.soinside.com 2019 - 2024. All rights reserved.