views:

37

answers:

1

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&lt;/url&gt;--&gt;
                    <!--<url>http://ws.xwebservices.com/XWebEmailValidation/V2/XWebEmailValidation.wsdl&lt;/url&gt;--&gt;
                    <url>http://mathertel.de/AJAXEngine/S02_AJAXCoreSamples/OrteLookup.asmx?WSDL&lt;/url&gt;
                </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 ?

+1  A: 

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"&gt;
  <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&lt;/url&gt;
  <repositories>
    <repository>
      <id>maven2-repository.dev.java.net</id>
      <name>Java.net Repository for Maven 2</name>
      <url>http://download.java.net/maven/2&lt;/url&gt;
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>maven2-repository.dev.java.net</id>
      <url>http://download.java.net/maven/2&lt;/url&gt;
    </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&lt;/wsdlUrl&gt;
              </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());
    }
}
Pascal Thivent