A simple webservice using Apache CXF

I have modified the previous application in http://cuppajavamattiz.com/2012/11/09/a-simple-restful-service/, to include a webservice using Apache CXF. The jars I included from the CXF distribution were:
httpclient-4.2.1.jar
httpcore-4.2.2.jar
httpcore-nio-4.2.2.jar
jaxb-impl-2.2.5.jar
neethi-3.0.2.jar
cxf-manifest.jar
geronimo-javamail_1.4_spec-1.7.1.jar
geronimo-jaxws_2.2_spec-1.1.jar
geronimo-jms_1.1_spec-1.1.1.jar
geronimo-servlet_3.0_spec-1.0.jar
httpasyncclient-4.0-beta3.jar
xmlschema-core-2.0.3.jar
I modified the web.xml to look this
Remove the REST specific configurations below so that the app only works with REST.

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xmlns=”http://java.sun.com/xml/ns/javaee&#8221; xmlns:web=”http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&#8221; xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&#8221; id=”WebApp_ID” version=”2.5″>

<display-name>REST Service</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/rest-services-config.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>restservices</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>restservices</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>spring-ws</servlet-name>
<servlet-class>>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
</web-app>

Added applicationContext.xml

Added cxf-servlet.xml

Added two classes:

package com.mattiz.ws.service;

import java.rmi.Remote;
import javax.jws.WebService;

@WebService(targetNamespace = "http:/com.mattiz/wsdl")
public interface MattizWebService extends Remote {
	public String getWelcomeString();
}
package com.mattiz.ws.service;

import javax.annotation.PostConstruct;
import javax.jws.WebMethod;
import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import com.mattiz.rest.service.MattizService;

@Service("mattizWebService")
@WebService(serviceName = "/MattizWebService", portName = "MattizWebServicePort", targetNamespace = "http:/com.mattiz/wsdl", endpointInterface = "com.mattiz.ws.service.MattizWebService")
public class MattizWebServiceImpl implements MattizWebService {

	@Autowired
	private MattizService mattizService;
	@WebMethod(operationName = "getWelcomeString", action = "urn:getWelcomeString")
	public String getWelcomeString() {
		String welcomeString = null;
		try {
			welcomeString = mattizService.getWelcomeString();
			return welcomeString;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	@PostConstruct
	public void init() {
	    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
	}
	public MattizService getMattizService() {
		return mattizService;
	}
	public void setMattizService(MattizService mattizService) {
		this.mattizService = mattizService;
	}
}

Note that the webservice is not created by Spring, so the mattizService bean is not autowired through spring directly, hence the init method to enable spring autowiring.

The wsdl can be viewed using the following url:

http://localhost:8080/RestServiceExampleWithJson/webservice/mattiz?wsdl

To test the webservice I have modified a client module described elsewhere on this blog http://cuppajavamattiz.com/2011/07/10/an-ejb3-webservice-with-generated-client-code/.

build-client.xml

The test class:

package com.mattiz.ws.client;

import com.mattiz.ws.test.bean.MattizWebService;
import com.mattiz.ws.test.bean._002fMattizWebService;

public class MattizWSClientTest {
	public static void main(String[] args) {
		try {
			MattizWebService port = new _002fMattizWebService().getMattizWebServicePort();
			System.out.println(port.getWelcomeString());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

The classes under com.mattiz.ws.test.bean package are automatically generated web service stubs by the build xml.
Run the client class as a standalone to test.
The result is:
Hallo Mattiz World!

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Comments are closed.

%d bloggers like this: