Web Services with Apache CXF

CXF framework is widely used to create web services. It supports 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.

CXF has some tools like wsdl2java witch generate web service code from WSDL. This tool is very useful but it integrates some code that usually we don’t control all its details.

Photo credit: Brian A Jackson

I wrote this article to presents a concise way to create a web service with CXF and Spring. This solution has the advantage to avoid generating extra code to integrate in your project.

1 – CXF dependency

If you are using Maven you can add dependency by adding the XML code below.

<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-frontend-jaxws</artifactId>
	<version>2.1.3</version>
</dependency>
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-transports-http</artifactId>
	<version>2.1.3</version>
</dependency>

2 – Web service implementation

First, add a web service interface. The code source bellow is a simple of web service that allows adding a customer to an existent JSF web application.

import javax.jws.WebService;
import com.mycompany.entity.Customer;

@WebService
public interface CustomerService {

	String SaveCustomer(Customer customer);

}


Next, write your web service implementation.

import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.mycompany.dao.ICustomerDao;
import com.mycompany.entity.Customer;

@Component("customerService")
@WebService(endpointInterface = "com.mycompany.service.CustomerService")
public class CustomerServiceImpl implements CustomerService {
	
@Autowired
private ICustomerDao customerDao;

	public String SaveCustomer(Customer customer) {
		customer.setCustomerId(null);
		customer.setCustomerOrders(null);
		customerDao.save(customer);
		return "saved";
	}	
}


Finally, add spring configuration to configure the web service end point and client.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

	<jaxws:endpoint id="customerServiceEndPoint" implementor="#customerService"
		address="/CustomerService" />

	<jaxws:client id="customerServiceClient" serviceClass="com.mycompany.service.CustomerService"
		address="http://localhost:8080/myApp_JSF_CXF/ws/CustomerService" />
</beans>

3 – Web service test

Test with SoapUi
You can use SoapUi to test your web service. In this case you don’t need to implemente a service client.

SoapUi needs a wsdl file. You can provide this URL http://localhost:8080/myApp_JSF_CXF/ws/CustomerService?wsdl.

Test with JUnit

Testing a web service with junit it’s more likely to be an integration test than a unit test because it depends on other resources. You will need to start your application server and deploy your web service before running your test.

The code source bellow uses the web service client to test the web service developed in this article.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/application-context-test.xml"})
public class CustomerServiceTest {
	
	@Autowired
	@Qualifier("customerServiceClient")
	private CustomerService customerServiceClient;

	@Test
	public void SaveCustomerClientTest() {
		Customer customer = new Customer(25, "nameTest","adresse", "city", "state", "123", "0606060606", null);
		assertEquals("saved", customerServiceClient.SaveCustomer(customer));	
	}
}

Enhance your pictures with Gimp

Gimp is an open-source alternative for Photoshop. It has all basic picture editing tools and many advanced features. It is a freely distributed program for such tasks as photo retouching, image composition and image authoring. It is compatible with most operating systems (Windows, Linux, MacOS X …).

Gimp has many tools to manage color saturation, brightness, contrast … The picture bellow presents the GIMP user interface and how to adjust a picture color curve.

In this portrait image I made some other changes in the background of the picture like hiding some part and applying a blur filter. There is a lot of free tutorials about Gimp. To make those changes I used this link : http://gimpguru.org/tutorials

Original picture

Modified picture

I think GIMP is very good tool to edit and enhance pictures. I find it better than pictures editing software provided with my camera which are a little bit basic and which change from one constructor to another.