[英]Annotating CXF (wsdl2java) generated package
我需要添加包级别注释(XmlJavaTypeAdapters类型适配器)。 问题是,当我运行wsdl2java时,它将为该软件包生成package-info.java文件。
当我尝试添加自己的package-info.java时,出现错误消息:“类型package-ingo已经定义”。
有没有一种方法可以将我的注释注入package-info.java中? 也许还有其他想法吗?
谢谢
经过一番研究,我使用了外部映射文件。 对于所有与我类似的问题,我在下面描述了所发现的问题。
如果使用“ cxf-codegen-plugin”从WSDL生成源代码,则不能将解决方案与package-info.java一起使用。 这是因为生成的代码可能已经包含此文件。 您也不能将注释添加到类中,因为注释已生成。 唯一的解决方案是提供您自己的映射器。
首先,您必须编写自定义映射器。 之后,您应该定义xjb映射文件,最后将其他配置添加到pom.xml。 您可以在此处阅读有关前两个步骤的信息 。
要将外部映射文件添加到cxf-codegen-plugin,必须在插件定义的配置节点中添加以下内容:
<defaultOptions>
<bindingFiles>
<bindingFile>${basedir}/src/main/resources/mapping.xjb</bindingFile>
</bindingFiles>
<noAddressBinding>true</noAddressBinding>
</defaultOptions>
请注意,您不应按此处所述将额外的参数传递给xjc,因为它将不起作用。
希望这对任何人都有帮助:)
我从没有尝试过,但是您可以尝试在wsdl2java命令中添加-xjc-npa标志。 从理论上讲,这告诉XJC不要生成package-info.java,而是将所有名称空间等粘贴在需要的所有其他元素上。
您可以内联WSDL或作为单独的外部绑定文件来提供JAXB“绑定”,并且JAXB将生成适当的适配器和所需的包级注释。 有关示例,请参见此问题 。
我还需要在生成的代码中添加注释。 在生成Java类之后,我使用了maven-replacer-plugin来执行此操作。 您可以使用此解决方案来修改任何出现的文件。
这是相关的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>
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.