0

我正在尝试了解 jax-ws 并创建了一个简单的自下而上 HelloWorld 网络服务来训练自己:

import org.jboss.annotation.ejb.LocalBinding;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@WebService(serviceName = "HelloWorldWebServiceExperiment",
    portName = "HelloWorldPort",
    name = "HelloWorld")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, 
    use = SOAPBinding.Use.LITERAL, 
    parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
@Stateless
@Remote
@LocalBinding(jndiBinding = "training.webservice.HelloWorldWebServiceRemote/local")

public class HelloWorldWebServiceBean {
    private static final String HELLO_WORLD = "Hello World";
    @WebMethod
    public String helloWorld() {
        return HELLO_WORLD;
    }
}

当我部署这个网络服务时,我可以访问 wsdl,所以这似乎工作正常。然后我尝试生成一个 web 服务客户端。为此,我需要创建存根,我尝试使用 maven 插件 jaxws-maven-plugin 创建它。这是我的 pom.xml:

<?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>training.webservice</groupId>
<artifactId>HelloWorldWebServiceExperiment</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
    <wsdlPath>${project.build.directory}</wsdlPath>
</properties>

<build>
    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <webResources>
                    <resource>
                        <targetPath>WEB-INF</targetPath>
                        <directory>src/main/webapp/WEB-INF</directory>
                        <includes>
                            <include>web.xml</include>
                        </includes>
                        <filtering>true</filtering>
                    </resource>
                </webResources>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxws-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>genwsdl</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>wsgen</goal>
                    </goals>
                    <configuration>
                        <genWsdl>true</genWsdl>
                        <sei>training.webservice.HelloWorldWebServiceBean</sei>
                    </configuration>
                </execution>
                <execution>
                    <id>consumeWsdlForStubs</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>wsimport</goal>
                    </goals>
                    <configuration>
                        <wsdlDirectory>target/jaxws/wsgen/wsdl</wsdlDirectory>
                        <wsdlFiles>
                            <wsdlFile>HelloWorldWebServiceExperiment.wsdl</wsdlFile>
                        </wsdlFiles>
                        <keep>true</keep>
                        <destDir>target/classes</destDir>
                        <sourceDestDir>target/stubs</sourceDestDir>
                        <verbose>true</verbose>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <executions>
                <execution>
                    <id>package-wsclient-jars</id>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <classesDirectory>target/stubs</classesDirectory>
                        <classifier>wsclient</classifier>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

<dependencies>
    <!-- JAX-WS Annotations -->
    <dependency>
        <groupId>javax.jws</groupId>
        <artifactId>jsr181-api</artifactId>
        <version>1.0-MR1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>jboss</groupId>
        <artifactId>jbossws-spi</artifactId>
        <version>1.0.0.GA</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss</groupId>
        <artifactId>jboss-ejb3x</artifactId>
        <version>4.2.2</version>
        <!--<scope>provided</scope>-->
    </dependency>
    <dependency>
        <groupId>jboss</groupId>
        <artifactId>jboss-annotations-ejb3</artifactId>
        <version>4.2.2.GA</version>
        <!--<scope>provided</scope>-->
    </dependency>
    <dependency>
        <groupId>org.jboss</groupId>
        <artifactId>jboss-jaxws</artifactId>
        <version>4.2.2</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

Build with maven 将生成以下输出和带有生成的 .java 文件的 .jar 文件:

> mvn clean install
[INFO] Scanning for projects...
[INFO]         ------------------------------------------------------------------------
[INFO] Building Unnamed - training.webservice:HelloWorldWebServiceExperiment:war:1.0-SNAPSHOT
[INFO]    task-segment: [clean, install]
[INFO]     ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory ---/HelloWorldWebServiceExperiment/target
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] [compiler:compile {execution: default-compile}]
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to ---/HelloWorldWebServiceExperiment/target/classes
[INFO] [compiler:compile {execution: default}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [jaxws:wsgen {execution: genwsdl}]
[INFO] [jaxws:wsimport {execution: consumeWsdlForStubs}]
[INFO] Processing: ---/HelloWorldWebServiceExperiment/target/jaxws/wsgen/wsdl/HelloWorldWebServiceExperiment.wsdl
[INFO] jaxws:wsimport args: [-s, ---/    /HelloWorldWebServiceExperiment/target/jaxws/wsimport/java, -d, ---/HelloWorldWebServiceExperiment/target/classes, -verbose, -Xnocompile, ---/HelloWorldWebServiceExperiment/target/jaxws/wsgen/wsdl/HelloWorldWebServiceExperiment.wsdl]
parsing WSDL...


generating code...

training/webservice/HelloWorld.java
training/webservice/HelloWorldResponse.java
training/webservice/HelloWorldWebServiceExperiment.java
training/webservice/HelloWorld_Type.java
training/webservice/ObjectFactory.java
training/webservice/package-info.java
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory ---/HelloWorldWebServiceExperiment/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [surefire:test {execution: default-test}]
[INFO] No tests to run.
[INFO] [war:war {execution: default-war}]
[INFO] Packaging webapp
[INFO] Assembling webapp[HelloWorldWebServiceExperiment] in [---/HelloWorldWebServiceExperiment/target/HelloWorldWebServiceExperiment-1.0-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp webResources[---/HelloWorldWebServiceExperiment/src/main/webapp/WEB-INF] to[---/HelloWorldWebServiceExperiment/target/HelloWorldWebServiceExperiment-1.0-SNAPSHOT]
[INFO] Copying webapp resources[---/HelloWorldWebServiceExperiment/src/main/webapp]
[INFO] Webapp assembled in[33 msecs]
[INFO] Building war: ---/HelloWorldWebServiceExperiment/target/HelloWorldWebServiceExperiment-1.0-SNAPSHOT.war
[INFO] [jar:jar {execution: package-wsclient-jars}]
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] Building jar: ---/HelloWorldWebServiceExperiment/target/HelloWorldWebServiceExperiment-1.0-SNAPSHOT-wsclient.jar
[INFO] [install:install {execution: default-install}]
[INFO] Installing ---/HelloWorldWebServiceExperiment/target/HelloWorldWebServiceExperiment-1.0-SNAPSHOT.war to ~/.m2/repository/training/webservice/HelloWorldWebServiceExperiment/1.0-SNAPSHOT/HelloWorldWebServiceExperiment-1.0-SNAPSHOT.war
[INFO] Installing ---/HelloWorldWebServiceExperiment/target/HelloWorldWebServiceExperiment-1.0-SNAPSHOT-wsclient.jar to ~/.m2/repository/training/webservice/HelloWorldWebServiceExperiment/1.0-SNAPSHOT/HelloWorldWebServiceExperiment-1.0-SNAPSHOT-wsclient.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Tue Aug 25 09:31:20 CEST 2015
[INFO] Final Memory: 32M/487M
[INFO] ------------------------------------------------------------------------

编辑(为了精确):当我尝试在另一个项目中使用存根存档来创建 web 客户端时,(我使用 intellij idea 执行此操作),idea 会自动从我的 maven 存储库中找到该文件并建议给我,生成以下依赖项:

 <dependency>
        <groupId>training.webservice</groupId>
        <artifactId>HelloWorldWebServiceExperiment</artifactId>
        <version>1.0-SNAPSHOT-wsclient</version>
 </dependency>

但是,实际上没有一个生成的 .java 文件可以在代码中导入和使用!例如,想法在使用或导入时失败

import training.webservice.HelloWorld;

private training.webservice.HelloWorld helloWorld;

带有错误消息“无法解析符号'HelloWorld'”我做错了什么?这是否与我的存根仅包含 .java 文件但没有 .class 文件的事实有关?显然,插件 jaxws-maven-plugin 使用 -xnocompile 标志运行 wsimport,但我无法弄清楚如何配置 maven 不这样做,没有任何选项保留,destDir 或 sourceDestDir 对此有任何影响!还是这完全无关紧要,因为导入存根的项目应该能够自己编译它们,而问题出在其他地方?任何帮助将不胜感激!

4

1 回答 1

0

我找到了一个临时工作:我手动生成了一个客户端存根 .jar 文件。由于我的公司仍在使用 Java 1.5,我必须安装 Metro 才能使用 wsimport(我选择了 1.6.2 版),然后我可以调用:

wsimport target/generated-sources/wsdl/HelloWorldWebServiceExperiment.wsdl -d target/generated-sources/wsdl/ -keep

之后我有 .java 和 .class 文件,我可以手动打包到 .jar 文件中,然后我将该 .jar 移动到我的 webservice 客户端项目的 ressource 文件夹中,并将依赖项手动添加到我的 pom.xml 中:

        <dependency>
            <groupId>training.webservice</groupId>
            <artifactId>HelloWorldWebServiceExperiment</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/client.jar</systemPath>
    </dependency>

我更喜欢从 maven 自动构建和部署,我现在确定问题必须出在我的 maven 设置中,但是我仍然不知道如何做得更好。至少我现在可以继续处理 web 服务客户端,稍后再重新讨论这个问题!

于 2015-08-25T12:57:54.827 回答