使用axis和maven访问Web服务

时间:2010-08-13 17:06:25

标签: web-services maven-2 axis wsdl2java webservice-client

我试图弄清楚如何使用Axis访问Java中的Web服务。

据我所知,这就是我需要做的事情:

  1. 使用WSDL File + Axis工具生成Java文件。
  2. 编译并打包生成的Java文件,然后使用这些对象的连接方法来使用这些对象。
  3. 试图这样做,这就是我被困的地方:

    我从http://www.service-repository.com/选择了一个随机的Web服务 我以下列方式使用了axistools-maven-plugin:

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>axistools-maven-plugin</artifactId>
                <configuration>
                    <urls>
                        <!--<url>http://soap.amazon.com/schemas2/AmazonWebServices.wsdl</url>-->
                        <!--<url>http://ws.xwebservices.com/XWebEmailValidation/V2/XWebEmailValidation.wsdl</url>-->
                        <url>http://mathertel.de/AJAXEngine/S02_AJAXCoreSamples/OrteLookup.asmx?WSDL</url>
                    </urls>
                    <!--<sourceDirectory>${project.build.sourceDirectory}/wsdl</sourceDirectory>-->
                    <packageSpace>com.company.wsdl</packageSpace>
                    <testCases>true</testCases>
                    <serverSide>true</serverSide>
                    <subPackageByFileName>true</subPackageByFileName>
                    <outputDirectory>${project.build.directory}/src/generated-sources</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    

    问题在于:

    我可以成功运行mvn generate-sources,它确实生成了Java文件。但我似乎无法编译这些Java文件。 当我运行mvn clean install时,它给了我一堆编译错误。我错过了什么步骤?

1 个答案:

答案 0 :(得分:1)

根据您对我的评论之一的回答,我的建议是使用JAR-WS实现,如JAX-WS RI - 包含在Java 6中 - 或Apache CXF(两者都是IMO更好WS堆栈比过时的Axis)。

以下是基于JAX-WS RI及其jaxws-maven-plugin

的示例
<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>com.stackoverflow</groupId>
  <artifactId>Q3479139</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>Q3479139</name>
  <url>http://maven.apache.org</url>
  <repositories>
    <repository>
      <id>maven2-repository.dev.java.net</id>
      <name>Java.net Repository for Maven 2</name>
      <url>http://download.java.net/maven/2</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>maven2-repository.dev.java.net</id>
      <url>http://download.java.net/maven/2</url>
    </pluginRepository>
  </pluginRepositories>
  <dependencies>
    <dependency>
      <groupId>com.sun.xml.ws</groupId>
      <artifactId>jaxws-rt</artifactId>
      <version>2.2.1</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxws-maven-plugin</artifactId>
        <version>1.12</version>
        <executions>
          <execution>
            <goals>
              <goal>wsimport</goal>
            </goals>
            <configuration>
              <wsdlUrls>
                <wsdlUrl>http://ws.xwebservices.com/XWebEmailValidation/V2/XWebEmailValidation.wsdl</wsdlUrl>
              </wsdlUrls>
              <!-- The name of your generated source package -->
              <packageName>com.example.myschema</packageName>
              <!-- generate artifacts that run with JAX-WS 2.0 runtime -->
              <target>2.0</target>
              <!-- Specify where to place generated source files -->
              <sourceDestDir>${project.build.directory}/generated-sources/wsimport</sourceDestDir>
            </configuration>
          </execution>
        </executions>
        <!-- if you want to use a specific version of JAX-WS, you can do so like this -->
        <dependencies>
          <dependency>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>jaxws-tools</artifactId>
            <version>2.2.1</version>
          </dependency>
        </dependencies>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

这是一个非常基本的测试用例(maven项目的一部分),演示了使用生成的类调用Web服务:

package com.example.myschema;

import junit.framework.TestCase;

public class EmailValidationTest extends TestCase {

    XWebEmailValidationInterface service = new EmailValidation().getEmailValidation();
    ValidateEmailRequest request = new ValidateEmailRequest();
    ValidateEmailResponse response = null;

    public void testEmails() {
        request.setEmail("foo@bar.com");
        response = service.validateEmail(request);
        assertEquals("EMAIL_SERVER_NOT_FOUND", response.getStatus());

        request.setEmail("foo@gmail.com");
        response = service.validateEmail(request);
        assertEquals("NOT_VALID", response.getStatus());
    }
}