Annotating CXF (wsdl2java) generated package

2019-05-31 04:40发布

I need to add package level annotation (XmlJavaTypeAdapters type adapter). The problem is that when I run wsdl2java it generates package-info.java file for that package.

When I try to add my own package-info.java I get error: "the type package-ingo is already defined".

Is there a way to inject my annotation to package-info.java?? Maybe any other ideas?

thanks

4条回答
啃猪蹄的小仙女
2楼-- · 2019-05-31 05:27

I needed to add an annotation to generated code as well. I used the maven-replacer-plugin to do this just after the java classes were generated. You could use this solution to modify any file that comes out.

Here's the relevant pom.xml bit:

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

Hope this helps!

查看更多
beautiful°
3楼-- · 2019-05-31 05:28

I've never tried this, but you could try adding an -xjc-npa flag to the wsdl2java command. In theory, that tells XJC to not generate a package-info.java and instead stick all the namespaces and such on all the other elements where it's needed.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-05-31 05:41

You can supply JAXB "bindings", either inline in the WSDL or as a separate external binding file, and JAXB will generate the appropriate adapters and the required package-level annotations. See this question for an example.

查看更多
欢心
5楼-- · 2019-05-31 05:44

After some research I used external mapping file. For all that have similar problem to mine I have described below what I have found.

If you are using "cxf-codegen-plugin" for generating source code from WSDL you can't use solution with package-info.java. This is because generated code propably will already contain this file. You cannot also add annotation to your class because it is generated. The only solution is to provide your own mapper.

First of all you have to write custom mapper. After that you should define xjb mapping file and finally add additional configuration to your pom.xml. You can read about first two steps here.

To add external mapping file to cxf-codegen-plugin you have to add something like this to configuration node in plugin definition:

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

Note that you should not pass extra parameters to xjc as described here because it will not work.

Hope this will help anybody :)

查看更多
登录 后发表回答