February 28, 2010

Using JAX-WS in a Web Archive, as a Servlet, Running on JBoss 5.1

In my previous blog I showed how to get started with JAX-WS in a EJB archieve, in this blog I will show you how to do the same thing, but in a web archive instead, as a Servlet. If you think about it, the two different approaches ends with the same result – a Servlet that answer HTTP request with SOAP content.

Now lets create a simple web archieve with Maven archetype plugin.

$ mvn archetype:create -DgroupId=se.msc.example -DartifactId=ws-web -DarchetypeArtifactId=maven-archetype-webapp


Now lets add EJB dependency and JBoss Maven plugin that will do our deployment work.

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
 <modelversion>4.0.0</modelVersion>
 <groupid>se.msc.example</groupId>
 <artifactid>ws-web</artifactId>
 <packaging>war</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>ws-web Maven Webapp</name>
 <url>http://www.msc.se/example/ws-web</url>
 <developers>
  <developer>
   <id>magnus.k.karlsson</id>
   <name>Magnus K Karlsson</name>
   <email>magnus.k.karlsson@msc.se</email>
   <organization>MSC</organization>
  </developer>
 </developers>
 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <jboss.home>/home/magnus/applications/jboss-5.1.0.GA-jdk6</jboss.home>
 </properties>
 <dependencies>
  <!-- sun's ejb 3.0 api -->
  <dependency>
   <groupid>javax.ejb</groupId>
   <artifactid>ejb-api</artifactId>
   <version>3.0</version>
   <scope>provided</scope>
  </dependency>
  <!-- jboss logging impl (jboss 5.1.0 GA) -->
  <dependency>
   <groupid>log4j</groupId>
   <artifactid>log4j</artifactId>
   <version>1.2.14</version>
   <scope>provided</scope>
  </dependency>
  <!-- unit testing -->
  <dependency>
   <groupid>junit</groupId>
   <artifactid>junit</artifactId>
   <version>4.7</version>
   <scope>test</scope>
  </dependency>
 </dependencies>
 <build>
  <finalname>ws-web</finalName>
  <plugins>
   <!-- to compile with jdk 1.6 -->
   <plugin>
    <groupid>org.apache.maven.plugins</groupId>
    <artifactid>maven-compiler-plugin</artifactId>
    <version>2.1</version>
    <configuration>
     <source>1.6</source>
     <target>1.6</target>
     <debug>true</debug>
     <debuglevel>lines,vars,source</debuglevel>
     <optimize>true</optimize>
     <showdeprecation>true</showDeprecation>
     <showwarnings>true</showWarnings>
    </configuration>
   </plugin>
            <!-- plugin to harddeploy, hardundeploy, start and stop jboss -->
            <plugin>
                <groupid>org.codehaus.mojo</groupId>
                <artifactid>jboss-maven-plugin</artifactId>
                <version>1.4</version>
                <configuration>
                    <jbosshome>${jboss.home}</jbossHome>
                    <servername>default</serverName>
                    <port>8080</port>
                </configuration>
            </plugin>   
  </plugins>
 </build>
</project>


Then we continue with our Web Service class.

package se.msc.example.ws.web;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class PersonServiceBean {

 @WebMethod
    public String getName() {
     return "Magnus K Karlsson";
    }
}


Not that we don't need to define a interface. Then we write our web.xml, that points to our concrete Web Service class.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 version="2.5">

 <display-name>MSC Demo Web Service</display-name>
 <servlet>
    <servlet-name>PersonServiceBean</servlet-name>
    <servlet-class>se.msc.example.ws.web.PersonServiceBean</servlet-class>
 </servlet>
 <servlet-mapping>
    <servlet-name>PersonServiceBean</servlet-name>
    <url-pattern>/personServiceBean</url-pattern>
 </servlet-mapping>
</web-app>


Finally we need to add the JBoss specific web deployment descriptor. Note the location of the file – src/main/webapp/META-INF/jboss-web.xml, i.e. not in the WEB-INF directory, beside the web.xml.

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web xmlns="http://www.jboss.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd"
 version="5.1">
 
 <!-- Read about Schema documentation in $JBOSS_HOME/docs/schema/jboss-web_5_1.xsd -->
</jboss-web>    


Now we are ready to build and deploy our archieve

$ mvn clean install jboss:harddeploy


Then we start our JBoss server and opens a web browser pointing to http://localhost:8080/ws-web/personServiceBean?wsdl and checks that the web service is working. Instead of building a web service client by hand, we use the soapUI tool.

 

 

Now soapUI has created us a testcase that we can double click and click the green play button on.

No comments: