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.

About these ads

About javacodeblog

J2EE Developer
This entry was posted in Webservice, Webservices and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s