Creating a Webservice using the “maven-jaxws-tools-plugin”

This article may help you to create and deploy a Webservice using the “maven-jaxws-tools-plugin” from JBoss. First of all make sure that the JBoss Maven Repsoitory (including snapshots) is added to your settings.xml, then create a Maven-Multi-Module Project. The Parent Project’s pom.xml should contain two modules and may look like this:

  <groupId>de.test.package</groupId>
  <artifactId>ws-service</artifactId>
  <packaging>pom</packaging>
  <version>0.0.1-SNAPSHOT</version> 
  <modules>
    <module>ws-service-ejb</module>
    <module>ws-service-ear</module>
  </modules>

The Webservice will be located in the ejb-Module, wich will then be wrapped in an EAR-Archive for Deployment. Let’s have a look at the ejb-Module’s pom.xml:

    <parent>
        <artifactId>ws-service</artifactId>
        <groupId>de.test.package</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
   
    <groupId>de.test.package</groupId>
    <artifactId>ws-service-ejb</artifactId>
    <packaging>ejb</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>ws-service-ejb</name>   
  
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.ws.native</groupId>
            <artifactId>jbossws-native-core</artifactId>
            <version>3.2.2.GA</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
   
    <build>
        <finalName>ws-service-ejb</finalName>
        <plugins>
            <plugin>
                <groupId>org.jboss.ws.plugins</groupId>
                <artifactId>maven-jaxws-tools-plugin</artifactId>
                <version>1.0.1-SNAPSHOT</version>
                <configuration>
                    <verbose>true</verbose>
                    <endpointClass>org.ws.service.ejb.TestWebService</endpointClass>
                    <generateWsdl>true</generateWsdl>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>wsprovide</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <ejbVersion>3.1</ejbVersion>
                </configuration>
            </plugin>
        </plugins>
    </build>

The Endpointclass-Tag should point to the Class annotated with the “@Webservice”-Annotation. Here is an Example for such a Class:



package org.ws.service.ejb;

import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import org.jboss.ws.annotation.EndpointConfig;

@WebService(name = "Hello"
        serviceName = "HelloService"
        targetNamespace = "http://org.domain.com/samples/wssec&#034;)
@Stateless
@EndpointConfig(configName = "Standard WSSecurity Endpoint")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class TestWebService implements TestWebserviceInterface{

  @WebMethod
  public TestEntity sayHello() {
    System.out.println("Say hello...");
    return new TestEntity("foooo");
  }

}

The Entity the Service is returning is a simple EJB3-Entity Bean:


package org.ws.service.ejb;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class TestEntity {

  @Id
  @GeneratedValue
  private long id;
  
  private String someValue;

  public TestEntity(String someValue) {
    super();
    this.someValue = someValue;
  }

  public TestEntity() {
    super();
  }

  public String getSomeValue() {
    return someValue;
  }

  public void setSomeValue(String someValue) {
    this.someValue = someValue;
  }

  public long getId() {
    return id;
  }
  
}

The Interface the Webservice is implementing is an EJB-Remote Interface:


package org.ws.service.ejb;

import javax.ejb.Remote;

@Remote
public interface TestWebserviceInterface {
  
  public TestEntity sayHello();

}

You mave have noticed the @EndpointConfig(configName = "Standard WSSecurity Endpoint")-Annotation in the Webservice-Class. This config must be named "standard-jaxws-endpoint-config.xml" and has to be placed in src/main/resources/META-INF with the following content:

<jaxws-config xmlns="urn:jboss:jaxws-config:2.0" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:javaee="http://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="urn:jboss:jaxws-config:2.0 jaxws-config_2_0.xsd">

   <endpoint-config>
      <config-name>Standard WSSecurity Endpoint</config-name>
      <post-handler-chains>
         <javaee:handler-chain>
            <javaee:protocol-bindings>##SOAP11_HTTP</javaee:protocol-bindings>
            <javaee:handler>
               <javaee:handler-name>WSSecurity Handler</javaee:handler-name>
               <javaee:handler-class>
		org.jboss.ws.extensions.security.jaxws.WSSecurityHandlerServer
		</javaee:handler-class>
            </javaee:handler>
         </javaee:handler-chain>
      </post-handler-chains>
   </endpoint-config>

</jaxws-config>


Now you can build the deployable using the ear-module of the project. This pom.xml should do the trick:

    <parent>
        <artifactId>ws-service</artifactId>
        <groupId>de.test.package</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>de.test.package</groupId>
    <artifactId>ws-service-ear</artifactId>
    <packaging>ear</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>ws-service-ear</name>
    <dependencies>
        <dependency>
            <groupId>de.test.package</groupId>
            <artifactId>ws-service-ejb</artifactId>
            <type>ejb</type>
            <version>0.0.1-SNAPSHOT</version>
            <exclusions>
                <exclusion>
                    <groupId>org.jboss.ws.native</groupId>
                    <artifactId>jbossws-native-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <generateApplicationXml>true</generateApplicationXml>
                    <modules>
                        <ejbModule>
                            <groupId>de.test.package</groupId>
                            <artifactId>ws-service-ejb</artifactId>
                        </ejbModule>
                    </modules>
                </configuration>
            </plugin>
        </plugins>
        <finalName>ws-service-ear</finalName>
    </build>

Locate the ear-Archive in the target Folder of the ear-module and deploy it to the JBoss AS.

Advertisements
Posted in Webservice, Webservices | Tagged , , , , , | Leave a comment

Creating a Webservice-Client using the “maven-jaxws-tools-plugin”

Creating a Webservice-Client using the JBoss Maven Plugin is pretty straight forward.
Create a new Maven Project and make sure the pom.xml looks like this and the JBoss Maven Repository (including snaphshots) is added in your settings.xml.

    <groupId>de.test.mytests</groupId>
    <artifactId>ws-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.jboss.jbossas</groupId>
            <artifactId>jboss-as-client</artifactId>
            <type>pom</type>
            <version>5.1.0.GA</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.ws.native</groupId>
            <artifactId>jbossws-native-client</artifactId>
            <version>3.3.0.Beta4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.jboss.ws.plugins</groupId>
                <artifactId>maven-jaxws-tools-plugin</artifactId>
                <version>1.0.0.GA</version>
                <configuration>
                    <wsdls>
                        <wsdl>http://*host*:*port*/*path*/WebServiceName?wsdl</wsdl>
                    </wsdls>
                    <targetPackage>de.test.generated.classes</targetPackage>
                    <sourceDirectory>src/main/java</sourceDirectory>
                    <extension>true</extension>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>wsconsume</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

The path in the <wsdl></wsdl> Tag should point to an actual existing/vaild WSDL location. In the src/resources/META-INF Folder add standard-jaxws-client-config.xml, wich should have the following entries:

<jaxws-config xmlns="urn:jboss:jaxws-config:2.0"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:javaee="http://java.sun.com/xml/ns/javaee"
              	xsi:schemaLocation="urn:jboss:jaxws-config:2.0 jaxws-config_2_0.xsd">

   <client-config>
      <config-name>Standard WSSecurity Client</config-name>
      <post-handler-chains>
         <javaee:handler-chain>
            <javaee:protocol-bindings>##SOAP11_HTTP</javaee:protocol-bindings>
            <javaee:handler>
               <javaee:handler-name>WSSecurityHandlerOutbound</javaee:handler-name>
               <javaee:handler-class>
		org.jboss.ws.extensions.security.jaxws.WSSecurityHandlerClient
		</javaee:handler-class>
            </javaee:handler>
         </javaee:handler-chain>
      </post-handler-chains>
   </client-config>

</jaxws-config>

The goal “wsconsume” will create all necessary Proxy- and DTO-Classes
to use the Webservice in the folder src/main/java. Check that there is a “xyzService.java” Class in the specified target package. Create a new instance of this Class and invoke desired methods. Assuming the Webservicename was HelloService, you can then invoke a method like the “sayHello”-Method of the HelloWebservice like this:


package de.test.main;

import de.test.generated.classes.HelloService;

public class CallWebservice {
  
  public static void main(String[] args) {    
    HelloService service = new HelloService();
    service.getHelloPort().sayHello();
          
  }
Posted in Webservice Client | Tagged , , , | Leave a comment