3

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

据我了解,这是我需要做的:

  1. 使用 WSDL File + Axis 工具生成 Java 文件。
  2. 编译和打包生成的 Java 文件,然后通过在这些对象上使用连接方法来使用这些对象。

在尝试这样做时,这就是我卡住的地方:

我从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 时,它给了我一堆编译错误。我错过了什么步骤?

4

1 回答 1

2

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

这是一个基于 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());
    }
}
于 2010-08-13T21:17:21.630 回答