Tuesday, October 22, 2013

JBoss CXF Web services

 
jboss cxf tutorialJBoss AS 6 ships with Apache CXF web services implementation. In this tutorial we will show how to create a simple Web service endpoint and how to deploy and test it with a client.

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.

Developing the Web service


Create a new Dynamic Web Project from Eclipse Menu and select Target runtime and configuration "JBoss AS 6"

If you don't have a recent version of JBoss Tools installed you will not be able to see in the JBoss Community server list JBoss AS 6. However choosing JBoss AS 5 (and pointing to JBoss AS 6 installation) will just work fine

We will add at first the Web services an interface which will be used by our implementation.
01.package com.sample;
02. 
03.import javax.jws.WebMethod;
04.import javax.jws.WebService;
05. 
06.@WebService
07.public interface Math {
08.@WebMethod
09.public long sum(long a, long b);
10.}

and this is the implementation class:
01.package com.sample;
02. 
03.import javax.jws.WebMethod;
04.import javax.jws.WebService;
05. 
06.@WebService(endpointInterface = "com.sample.Math", serviceName = "MathWS")
07. 
08.public class MathWS implements Math
09.{    
10. 
11.public long sum(long a, long b) {
12.System.out.println("Summing "+a+" + "+b);
13.return a+b;
14.}
15.}

Now it's time to register your Web service. Add your web service in web.xml:
01.xml version="1.0" encoding="UTF-8"?>
02.<web-app
03.version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
06.<servlet>
07.<servlet-name>MathWS</servlet-name>
08.<servlet-class>com.sample.MathWS</servlet-class>
09.</servlet>
10.<servlet-mapping>
11.<servlet-name>MathWS</servlet-name>
12.<url-pattern>/*</url-pattern>
13.</servlet-mapping>
14.</web-app>

A JAX-WS Endpoint can be also configured using Spring XML file in addition to using the JAX-WS APIs. Once you've created your server implementation, you simply need to provide the class name and an address.
Here's a sample jbossws-cxf.xml
01.<beans
05.xmlns:jaxws="http://cxf.apache.org/jaxws"
10. 
11.
12.<jaxws:endpoint id="MathWS" address="http://localhost:8080/Samplews" 
13.implementor="com.sample.MathWS">
14.<jaxws:invoker>
15.<bean class="org.jboss.wsf.stack.cxf.InvokerJSE"/>
16.</jaxws:invoker>
17.</jaxws:endpoint>
18. 
19.</beans>

For more details about configuring Apache CXF web services with Apache CXF configuration file, please refer tohttp://community.jboss.org/wiki/JBossWS-StackCXFUserGuide

Deploy the service. If everything compiled correctly, you should see in the list of the deployed services your mathWS service.


Point the browser at http://localhost:8080/jbossws/services and check it.
jboss cxf tutorial

Develop a CXF client.

Here's a minimal client implementation:
01.package com.sample;
02. 
03.import org.apache.cxf.interceptor.*;
04.import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
05. 
06.public class Client {
07. 
08.public static void main(String args[]) throws Exception {
09. 
10.JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
11. 
12.factory.getInInterceptors().add(new LoggingInInterceptor());
13.factory.getOutInterceptors().add(new LoggingOutInterceptor());
14.factory.setServiceClass(Math.class);
15.factory.setAddress("http://localhost:8080/Samplews");
16.Math client = (Math) factory.create();
17. 
18.System.out.println("Server said: " + client.sum(3,4));
19. 
20.}
21. 
22.}


Notice the use of JaxWsProxyFactoryBean which is a factory for creating JAX-WS proxies. This class provides access to the internal properties used to set-up proxies. Using it provides more control than the standard JAX-WS APIs.<

No comments: