Building Web services on JBoss AS 7

Hi guys, here is my post regarding how to build web services on JBoss AS 7.x version. This post is fully self explanatory please follow the comments on the code.

step 1: create dynamic web project in eclipse with Jboss AS 7 as runtime configuration
step 2: in “src” folder code an interface as follows.

/**
 *
 */
package com.spark.webservice;

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

/**
 * @author Mantha Pavan Kumar
 *
 */
@WebService
public interface TemperatureConverter {

	@WebMethod
	public double convertCelsiusToFahrenheit(double value);
	@WebMethod
	public double convertFahrenheitToCelsius(double value);
}

step 3: code an implementation of the above interface as below.

/**
 *
 */
package com.spark.webservice.impl;

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

import com.spark.webservice.TemperatureConverter;

/**
 * @author Mantha Pavan Kumar
 *
 */
@WebService(endpointInterface="com.spark.webservice.TemperatureConverter",serviceName = "TemperatureConverter")
public class TemperatureConverterImpl implements TemperatureConverter{

	@Override
	@WebMethod
	public double convertCelsiusToFahrenheit(double value) {
		return value*(9/5)+32;
	}

	@Override
	@WebMethod
	public double convertFahrenheitToCelsius(double value) {
		return (value-32)*(5/9);
	}

}

step 4: now we deploy our web service as servlet on jboss, so lets code web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="jbosswebservices" version="2.5">
	
	<servlet>
		<servlet-name>webService</servlet-name>
		<servlet-class>com.spark.webservice.impl.TemperatureConverterImpl</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>webService</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>

</web-app>

step 5: once step 4 is done lets deploy it in the JBoss AS 7 in the “standalone/deployments” folder
step 6: start the server:

Note: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 standalone-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

once deployed and the server started without any error open the server admin-console, in the web services menu u should see as below

Name Context
webService JBossWebServices

Web Service


Name: webService Context: JBossWebServices
Class: TemperatureConverterImpl Type: JAXWS_JSE
WSDL Url: http://localhost:8080/JBossWebServices?wsdl

Now its time to code the web service client.

step 1: download the following jar files:
a)cxf-bundle-2.0.8.jar,
b)org.apache.servicemix.bundles.jaxb-impl-2.1.6_1.jar,
c)org.apache.servicemix.bundles.xmlresolver-1.2_1.jar,
d)org.apache.servicemix.bundles.xmlschema-1.4.3_1.jar.

and call the web service as below.

/**
 * 
 */
package com.spark.webservice.test;

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

import com.spark.webservice.TemperatureConverter;


/**
 * @author Mantha Pavan Kumar
 *
 */
public class TemperatureConverterTest {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
		factoryBean.getInInterceptors().add(new LoggingInInterceptor());
		factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
		factoryBean.setServiceClass(TemperatureConverter.class);
		factoryBean.setAddress("http://localhost:8080/JBossWebServices");
		TemperatureConverter converter = (TemperatureConverter)factoryBean.create();
		System.out.println(converter.convertCelsiusToFahrenheit(10));
	}
}

The out put should look as below:

Mar 11, 2013 2:47:40 PM org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
INFO: Creating Service {http://webservice.spark.com/}TemperatureConverterService from class com.spark.webservice.TemperatureConverter
Mar 11, 2013 2:47:42 PM org.apache.cxf.interceptor.LoggingOutInterceptor$LoggingCallback onClose
INFO: Outbound Message
---------------------------
Encoding: UTF-8
Headers: {SOAPAction=[""], Accept=[*]}
Messages: 
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:convertCelsiusToFahrenheit xmlns:ns1="http://webservice.spark.com/"><arg0>10.0</arg0></ns1:convertCelsiusToFahrenheit></soap:Body></soap:Envelope>
--------------------------------------
Mar 11, 2013 2:47:44 PM org.apache.cxf.interceptor.LoggingInInterceptor logging
INFO: Inbound Message
----------------------------
Encoding: UTF-8
Headers: {content-type=[text/xml;charset=UTF-8], Date=[Mon, 11 Mar 2013 09:17:44 GMT], Content-Length=[252], Server=[Apache-Coyote/1.1]}
Messages: 
Message:

Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:convertCelsiusToFahrenheitResponse xmlns:ns2="http://webservice.spark.com/"><return>42.0</return></ns2:convertCelsiusToFahrenheitResponse></soap:Body></soap:Envelope>
--------------------------------------
42.0

Happy Coding 🙂

Advertisements

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