首先,我使用 java 创建了简单的 Web 服务,然后为该 Web 服务生成了 wsdl,并使用 SoapUI 进行了测试,因此它工作正常。之后,现在我尝试从之前生成的 wsdl 文件生成 java 类。
这是我的 pom.xml 文件配置。
<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">
<parent>
<artifactId>webService</artifactId>
<groupId>WebService</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>contractFirst</artifactId>
<packaging>jar</packaging>
<name>contractFirst</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<cxf.version>2.2.3</cxf.version>
</properties>
<build>
<!-- Generate Java classes from WSDL during build -->
<plugins>
<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>${basedir}/target/generated/src/main/java</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/OrderProcess.wsdl</wsdl>
<extraargs>
<extraarg>-server</extraarg>
</extraargs>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Add generated sources - avoids having to copy generated sources to build location -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/target/generated/src/main/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<!-- Build the JAR with dependencies -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
当我尝试生成它时,我收到这样的错误。
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project contractFirst: Compilation failure: Compilation failure:
[ERROR] /home/contractFirst/target/generated/src/main/java/demo/order/OrderProcessService.java:[24,1] error: annotations are not supported in -source 1.3
[ERROR]
[ERROR] (use -source 5 or higher to enable annotations)
[ERROR] /home/contractFirst/target/generated/src/main/java/demo/order/OrderProcessService.java:[73,61] error: variable-arity methods are not supported in -source 1.3
[ERROR]
[ERROR] (use -source 5 or higher to enable variable-arity methods)
[ERROR] /home/contractFirst/target/generated/src/main/java/demo/order/ProcessOrderResponse.java:[29,1] error: annotations are not supported in -source 1.3
[ERROR]
[ERROR] (use -source 5 or higher to enable annotations)
[ERROR] /home/contractFirst/target/generated/src/main/java/demo/order/ObjectFactory.java:[24,1] error: annotations are not supported in -source 1.3
[ERROR]
[ERROR] (use -source 5 or higher to enable annotations)
[ERROR] /home/contractFirst/target/generated/src/main/java/demo/order/ObjectFactory.java:[66,22] error: generics are not supported in -source 1.3
[ERROR]
[ERROR] (use -source 5 or higher to enable generics)
[ERROR] /home/contractFirst/target/generated/src/main/java/demo/order/Order.java:[31,1] error: annotations are not supported in -source 1.3
[ERROR]
[ERROR] (use -source 5 or higher to enable annotations)
[ERROR] /home/contractFirst/target/generated/src/main/java/demo/order/ProcessOrder.java:[28,1] error: annotations are not supported in -source 1.3
[ERROR]
所以我不明白这个-来源 5 是什么?
更新:根据@Simze的回答,上述错误消失了。现在我收到此错误。
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.0:compile (default-compile) on project contractFirst: Compilation failure
[ERROR] /home/contractFirst/target/generated/src/main/java/demo/order/OrderProcess_OrderProcessPort_Server.java:[17,34] cannot find symbol
[ERROR] symbol: class OrderProcessImpl
[ERROR] location: class demo.order.OrderProcess_OrderProcessPort_Server
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.0:compile (default-compile) on project contractFirst: Compilation failure
/home/contractFirst/target/generated/src/main/java/demo/order/OrderProcess_OrderProcessPort_Server.java:[17,34] cannot find symbol
symbol: class OrderProcessImpl
location: class demo.order.OrderProcess_OrderProcessPort_Server
wsdl 文件
最佳答案
终于找到解决办法了。 当我们为给定的 wsdl 文件生成 java 代码时,我们需要在 cxf-codegen-plugin
的配置部分指定要生成的内容...
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/OrderProcess.wsdl</wsdl>
<extraargs>
<extrrg>-impl</extrrg>
<extrrg>-server</extrrg>
</extraargs>
</wsdlOption>
</wsdlOptions>
.....
这里-impl表示将生成服务实现类,-server用于生成可用于公开服务的服务器组件。
关于java - 无法从 wsdl 文件生成 java 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33241675/