使用 axis 和 Maven 访问 Web 服务

发布于 2024-09-14 09:59:38 字数 2035 浏览 10 评论 0原文

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

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

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

在尝试执行此操作时,我陷入了困境:

我从 http://www 中选择了一个随机 Web 服务.service-repository.com/ 我按以下方式使用 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>

问题是:

我可以成功运行 mvngenerate-sources 并且它确实生成了 Java 文件。但我似乎无法编译这些Java 文件。 当我运行 mvn clean install 时,它给了我一堆编译错误。我缺少哪一步?

Im trying to figure out how to access Web Services in Java using Axis.

As far as I understand, Here's what I need to do :

  1. Use WSDL File + Axis tools to generate Java files.
  2. Compile and package generated Java files and then consume those objects by using connection methods on these.

In trying to do this, here's where I'm stuck:

I picked a random Web Service from http://www.service-repository.com/
I used the axistools-maven-plugin in the following manner:

<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>

Here's the issue:

I can successfully run mvn generate-sources and it does generate the Java files. But I can't seem to compile these Java files.
When I run mvn clean install it gives me a bunch of compile errors. What step am I missing ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不及他 2024-09-21 09:59:38

根据您对我的评论之一的回答,我的建议是使用 JAX-WS 实现,例如 JAX -WS RI - 包含在 Java 6 中 - 或 Apache CXF (两者在我看来都差不多)比过时的 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("[email protected]");
        response = service.validateEmail(request);
        assertEquals("EMAIL_SERVER_NOT_FOUND", response.getStatus());

        request.setEmail("[email protected]");
        response = service.validateEmail(request);
        assertEquals("NOT_VALID", response.getStatus());
    }
}

Based on your answer to one of my comment, my suggestion would be to use a JAX-WS implementation like JAX-WS RI - which is included in Java 6 - or Apache CXF (both are IMO much better WS stacks than the outdated Axis).

Here is an example based on JAX-WS RI and its 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>

And here is a very basic test case (part of the maven project) demonstrating the invocation of the web service using the generated classes:

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("[email protected]");
        response = service.validateEmail(request);
        assertEquals("EMAIL_SERVER_NOT_FOUND", response.getStatus());

        request.setEmail("[email protected]");
        response = service.validateEmail(request);
        assertEquals("NOT_VALID", response.getStatus());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文