简体   繁体   English

从多个WSDL文件生成Java文件

[英]Generating Java files from multiple WSDL files

I'm currently involved in a project where we have to integrate our system to an external system. 我目前正在参与一个项目,我们必须将系统集成到外部系统。 The company responsible for the external system have provided us multiple WSDL and corresponding XSD files (Over 100 WSDL files). 负责外部系统的公司为我们提供了多个WSDL和相应的XSD文件(超过100个WSDL文件)。 Each WSDL file contains information about various services and the XSD files contains information about the types used for each service. 每个WSDL文件包含有关各种服务的信息,而XSD文件包含有关用于每种服务的类型的信息。

The problem I'm currently facing is when I'm generating Java classes from these XSD files. 我当前面临的问题是从这些XSD文件生成Java类时。 Many of the XSD files contains the same type. 许多XSD文件包含相同的类型。 Lets say that almost all of these XSD files contains information about "User". 可以说,几乎所有这些XSD文件都包含有关“用户”的信息。 The User type is identical in all the XSD files and have the same namespace. 所有XSD文件中的User类型都相同,并且具有相同的名称空间。 When generating Java classes from these WSDL files and placing them in different packages, I end up with over hundred version of the same class. 当从这些WSDL文件生成Java类并将它们放在不同的包中时,我最终得到了同一类的一百多个版本。 If I put all the Java classes in the same package, I override ObjectFactory. 如果将所有Java类放在同一包中,则将覆盖ObjectFactory。

So when I generate Java classes from one WSDL file, the result will be in about hundred Java classes. 因此,当我从一个WSDL文件生成Java类时,结果将是大约一百个Java类。 Fifty of these classes are the same in all the other WSDL files and the rest are unique for just that WSDL file. 在所有其他WSDL文件中,这些类中的五十个是相同的,其余的对于该WSDL文件而言是唯一的。

My question is the following: What is the best practice to handle these kind of scenarios? 我的问题如下:处理此类情况的最佳实践是什么? Would it be possible to merge all these WSDL files into one file and then generate classes from that? 是否可以将所有这些WSDL文件合并为一个文件,然后从中生成类? Or when I'm generating the Java classes, would it be possible to extract all the common classes into a separate package? 或者,当我生成Java类时,是否可以将所有公共类提取到单独的程序包中?

At the moment we use Apache CXF and Maven (cxf-codegen-plugin) to generate our classes and then do some manual work. 目前,我们使用Apache CXF和Maven(cxf-codegen-plugin)生成我们的类,然后进行一些手动工作。 However, I believe there should be a more efficient method to handle this. 但是,我认为应该有一个更有效的方法来处理此问题。

I appreciate all the help I can get on the problem. 我很高兴能就此问题提供所有帮助。

In the jaxws-maven-plugin you can specify multiple wsdls at once: 在jaxws-maven-plugin中,您可以一次指定多个wdls:

                    <configuration>
                        <keep>true</keep>
                        <extension>true</extension>
                        <catalog>XXX-models/catalog.xml</catalog>
                        <packageName>com.yyy.client</packageName>
                        <wsdlUrls>
                            <wsdlUrl>${basedir}/xml-resources/web-service-references/JobSession/wsdl/JobSessionService.wsdl</wsdlUrl>
                            <wsdlUrl>${basedir}/xml-resources/web-service-references/ZZZWSService/wsdl/ZZZWSService.wsdl</wsdlUrl>
....

Looking at the documentation , it looks like you can do this with CXF, too. 查看文档 ,您似乎也可以使用CXF做到这一点。 See the wsdlOptions element. 请参阅wsdlOptions元素。

If this doesn't help, you should post the relevant part of your pom.xml. 如果这样做没有帮助,则应发布pom.xml的相关部分。

This is how i managed to solve the problem. 这就是我设法解决问题的方式。

<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>
                    <configuration>
                        <sourceRoot>${project.basedir}/src/main/java</sourceRoot>

                        <wsdlOptions>
                            <!-- for debtProjections.wsdl -->

                            <wsdlOption>
                                <autoNameResolution>true</autoNameResolution>
                                <wsdl>${project.basedir}/src/main/resources/wsdl/debtProjections.wsdl</wsdl>
                                <wsdlLocation>classpath:/wsdl/debtProjections.wsdl</wsdlLocation>
                                <extraargs>
                                    <extraarg>-verbose</extraarg>
                                    <extraarg>-p</extraarg>
                                    <extraarg>com.mof.ws.debtprojections.service</extraarg>
                                </extraargs>

                            </wsdlOption>
                            <!-- for reference.wsdl -->
                            <wsdlOption>
                                <autoNameResolution>false</autoNameResolution>
                                <wsdl>${project.basedir}/src/main/resources/wsdl/reference.wsdl</wsdl>
                                <wsdlLocation>classpath:/wsdl/reference.wsdl</wsdlLocation>
                                <extraargs>
                                    <extraarg>-verbose</extraarg>
                                    <extraarg>-p</extraarg>
                                    <extraarg>com.mof.ws.reference.service</extraarg>
                                </extraargs>

                            </wsdlOption>
                            <!-- for dsbProjections.wsdl -->
                            <wsdlOption>
                                <autoNameResolution>false</autoNameResolution>
                                <wsdl>${project.basedir}/src/main/resources/wsdl/dsbProjections.wsdl</wsdl>
                                <wsdlLocation>classpath:/wsdl/dsbProjections.wsdl</wsdlLocation>
                                <extraargs>
                                    <extraarg>-verbose</extraarg>
                                    <extraarg>-p</extraarg>
                                    <extraarg>com.mof.ws.dbsprojections.service</extraarg>
                                </extraargs>

                            </wsdlOption>

                            <!-- for debtService?wsdl -->
                            <wsdlOption>
                                <autoNameResolution>false</autoNameResolution>
                                <wsdl>${project.basedir}/src/main/resources/wsdl/debtService.wsdl</wsdl>
                                <wsdlLocation>classpath:/wsdl/debtService.wsdl</wsdlLocation>
                                <extraargs>
                                    <extraarg>-verbose</extraarg>
                                    <extraarg>-p</extraarg>
                                    <extraarg>com.mof.ws.debtservice.service</extraarg>
                                </extraargs>
                            </wsdlOption>
                        </wsdlOptions>
                    </configuration>
                    <goals>
                        <goal>wsdl2java</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM