Developing Web services on JBoss AS 7

webservicesIn this tutorial we will show how to deploy a JAX-WS Web service on JBoss AS 7. You will see how you can easily deploy and test a Web service in the new application server release.

In order to use Web services in the new application server release you need the following extension in your configuration file:

<extension module="org.jboss.as.webservices"/>					

If you are using JBoss AS 7.1.1 this extension is already included in the standalone.xml configuration file, otherwise earlier releases include it in the -preview.xml configuration files so for example if you want to start the standalone server and user webservices you need to use the –server-config option. For example:

standalone.bat --server-config=standalone-preview.xml  

JBoss AS 7 ships with Apache CXF web services implementation. Apache CXF is an open source services framework. CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI.

So let’s create a simple Web service contained in a Web application:

package com.sample;

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

@WebService
public interface Math {
 @WebMethod
 public long sum(long a, long b);
}

And this is the corresponding Web service implementation:

package com.sample;

import javax.jws.WebService;

@WebService(endpointInterface = "com.sample.Math", serviceName = "MathWS")

public class MathWS implements Math 
{     

  public long sum(long a, long b)
   {
    System.out.println("Summing "+a+" + "+b);
    return a+b;
   }
}

   
As this Web service is implemented as a Servlet, we need registering the Servlet into the web.xml as usual:

 <servlet>
 <servlet-name>MathWS</servlet-name>
 <servlet-class>com.sample.MathWS</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>MathWS</servlet-name>
 <url-pattern>/*</url-pattern>
 </servlet-mapping>  

Now deploy the Web application and verify from that the Web service has been published. (We have used a Web application named balance.war).

In AS7 you don’t have anymore the jbossws Web application but you can use the new Web management at http://localhost:9900/console

In the Web tab you can find the Web services link where you can see the list of the Web services published.
JBoss 7 Web service tutorial
Now writing an Apache CXF client is just a matter of seconds:

package com.sample;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class Client {

    public static void main(String args[]) throws Exception {

        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

        factory.getInInterceptors().add(new LoggingInInterceptor());
        factory.getOutInterceptors().add(new LoggingOutInterceptor());
        factory.setServiceClass(Math.class);
        factory.setAddress("http://localhost:8080/balance");
        Math client = (Math) factory.create();

        System.out.println("Server said: " + client.sum(3,4));

    }

}