我正在尝试将使用SOAP的应用程序移植到JAVA。我在使cxf-codegen-plugin读取来自第三方的WSDL时遇到问题,它实际上可以工作。我猜测问题在于WSDL不完全符合该标准,并且CXF无法完成解析。
例如,以http://www.littlebigextra.com/consume-secure-soap-web-service-spring-boot-application/的教程为例,我可以成功地解析以下WSDL https://osetesting.bizlinks.com.pe/ol-ti-itcpe/billService?wsdl和https://test.conose.pe/ol-ti-itcpe/billService.svc,但是无法处理https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl,如果这很有趣,我并没有陷入这个问题,因为最后一个应该作为前两个的参考。
我还在https://spring.io/guides/gs/consuming-web-service/上尝试过SpringBoot教程,它不会引发错误,但是也不会创建类。
这是我的pom(摘自http://www.littlebigextra.com/consume-secure-soap-web-service-spring-boot-application/)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test.wsdl</groupId>
<artifactId>test-sunat-soap-ws</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<cxf.version>3.1.10</cxf.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-policy</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-tools-common</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-security</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<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>
<additionalJvmArgs>
-Djavax.xml.accessExternalDTD=all
-Djavax.xml.accessExternalSchema=all
</additionalJvmArgs>
<sourceRoot>${basedir}/src/main/generated</sourceRoot>
<wsdlOptions>
<wsdlOption>
<extraargs>
<extraarg>-verbose</extraarg>
<extraarg>-p</extraarg>
<extraarg>sunat.generated</extraarg>
<extraarg>-exsh</extraarg>
<extraarg>true</extraarg>
</extraargs>
<!--<wsdl>https://osetesting.bizlinks.com.pe/ol-ti-itcpe/billService?wsdl</wsdl>-->
<!--<wsdl>https://test.conose.pe/ol-ti-itcpe/billService.svc</wsdl>-->
<wsdl>https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/src/main/generated</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
运行mvn generate-sources时,可以看到正在调用wsdl2java工具
wsdl2java -encoding UTF-8 -d C:\dev-tools\testbizlinksgetstatuscdr\src\main\generated -verbose -p sunat.generated -exsh true https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl
但是,我得到了以下堆栈跟踪
org.apache.cxf.tools.common.ToolException: java.io.IOException: Server returned HTTP response code: 503 for URL: https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?ns1.wsdl
at org.apache.cxf.tools.validator.internal.Stax2DOM.getDocument(Stax2DOM.java:64)
at org.apache.cxf.tools.validator.internal.Stax2DOM.getDocument(Stax2DOM.java:47)
at org.apache.cxf.tools.validator.internal.WSDLRefValidator.getWSDLDocument(WSDLRefValidator.java:168)
at org.apache.cxf.tools.validator.internal.WSDLRefValidator.getWSDLDocuments(WSDLRefValidator.java:185)
at org.apache.cxf.tools.validator.internal.WSDLRefValidator.isValid(WSDLRefValidator.java:231)
at org.apache.cxf.tools.validator.internal.WSDL11Validator.isValid(WSDL11Validator.java:134)
at org.apache.cxf.tools.wsdlto.frontend.jaxws.wsdl11.JAXWSDefinitionBuilder.validate(JAXWSDefinitionBuilder.java:207)
at org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.processWsdl(WSDLToJavaContainer.java:201)
at org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.execute(WSDLToJavaContainer.java:164)
at org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.execute(WSDLToJavaContainer.java:412)
at org.apache.cxf.tools.common.toolspec.ToolRunner.runTool(ToolRunner.java:105)
at org.apache.cxf.tools.wsdlto.WSDLToJava.run(WSDLToJava.java:113)
at org.apache.cxf.tools.wsdlto.WSDLToJava.run(WSDLToJava.java:86)
at org.apache.cxf.maven_plugin.wsdl2java.WSDL2JavaMojo.generate(WSDL2JavaMojo.java:414)
at org.apache.cxf.maven_plugin.AbstractCodegenMoho.execute(AbstractCodegenMoho.java:279)
at org.apache.cxf.maven_plugin.wsdl2java.WSDL2JavaMojo.execute(WSDL2JavaMojo.java:511)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:154)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:146)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:954)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:192)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: java.io.IOException: Server returned HTTP response code: 503 for URL: https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?ns1.wsdl
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at java.net.URL.openStream(URL.java:1045)
at org.apache.cxf.tools.validator.internal.Stax2DOM.getDocument(Stax2DOM.java:59)
... 37 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 32.681 s
[INFO] Finished at: 2019-07-18T15:21:38-05:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.cxf:cxf-codegen-plugin:3.3.2:wsdl2java (generate-sources) on project test-bizlinks-getstatuscdr: Execution generate-sources of goal org.apache.c
xf:cxf-codegen-plugin:3.3.2:wsdl2java failed:
[ERROR] Summary: Failures: 14, Warnings: 0
[ERROR]
[ERROR] <<< ERROR!
[ERROR] null
[ERROR] https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl:[29,1]
[ERROR] Caused by {http://service.sunat.gob.pe}[portType:billService][operation:sendSummary][output:sendSummaryResponse] not exist.
[ERROR] https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl:[27,1]
[ERROR] Caused by {http://service.sunat.gob.pe}[portType:billService][operation:sendSummary][input:sendSummaryRequest] not exist.
[ERROR] https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl:[25,1]
[ERROR] Caused by {http://service.sunat.gob.pe}[portType:billService][operation:sendSummary] not exist.
[ERROR] https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl:[23,1]
[ERROR] Caused by {http://service.sunat.gob.pe}[portType:billService][operation:sendPack][output:sendPackResponse] not exist.
[ERROR] https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl:[21,1]
[ERROR] Caused by {http://service.sunat.gob.pe}[portType:billService][operation:sendPack][input:sendPackRequest] not exist.
[ERROR] https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl:[19,1]
[ERROR] Caused by {http://service.sunat.gob.pe}[portType:billService][operation:sendPack] not exist.
[ERROR] https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl:[17,1]
[ERROR] Caused by {http://service.sunat.gob.pe}[portType:billService][operation:sendBill][output:sendBillResponse] not exist.
[ERROR] https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl:[15,1]
[ERROR] Caused by {http://service.sunat.gob.pe}[portType:billService][operation:sendBill][input:sendBillRequest] not exist.
[ERROR] https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl:[13,1]
[ERROR] Caused by {http://service.sunat.gob.pe}[portType:billService][operation:sendBill] not exist.
[ERROR] https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl:[11,1]
[ERROR] Caused by {http://service.sunat.gob.pe}[portType:billService][operation:getStatus][output:getStatusResponse] not exist.
[ERROR] https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl:[9,1]
[ERROR] Caused by {http://service.sunat.gob.pe}[portType:billService][operation:getStatus][input:getStatusRequest] not exist.
[ERROR] https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl:[7,1]
[ERROR] Caused by {http://service.sunat.gob.pe}[portType:billService][operation:getStatus] not exist.
[ERROR] https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl:[5,1]
[ERROR] Caused by {http://service.sunat.gob.pe}[portType:billService] not exist.
[ERROR]
[ERROR] -> [Help 1]
编辑:我包括了整个错误输出,因为我意识到在堆栈跟踪之后还有其他错误
URL:https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?ns1.wsdl的第503行是让我认为WSDL不完全兼容的原因(因为有?ns1.wsdl参数,而不仅仅是?wsdl)但是,如果我尝试访问该链接,我确实获得了与使用原始链接相同的WSDL,所以我根本不理解503错误。
运行第二个教程中的代码时,会收到以下消息
[INFO] --- maven-jaxb2-plugin:0.12.3:generate (default) @ sunat-consuming-web-service ---
[INFO] Up-to-date check for source resources [[https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl, file:/C:/dev-tools/sunatconsumingwebservice/pom.xml]] and taret resource
s [[]].
[WARNING] The URI [https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl] seems to represent an absolute HTTP or HTTPS URL. Getting the last modification timestamp is only po
ssible if the URL is accessible and if the server returns the [Last-Modified] header correctly. This method is not reliable and is likely to fail. In this case the last modification time
stamp will be assumed to be unknown.
[ERROR] Could not retrieve the last modification timestamp for the URI [https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl] from the HTTP URL connection. The [Last-Modifie
d] header was probably not set correctly.
[WARNING] Last modification of the URI [https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService?wsdl] is not known.
[INFO] Sources are not up-to-date, XJC will be executed.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
很酷,没有错误...但是也没有任何类。当我使用其他两个URL中的任何一个时,这些类将在target / xjc / sunat.wsdl中生成,并显示完全相同的消息。
我尝试将详细信息与mvn generate-sources -X一起使用,但是除了显示有关设置的其他消息外,没有出现错误或未生成类的更多信息。
如何获取任一代码来生成WS的类?