View RSS Feed

Spring Framework

Intro to Web Services with Spring

Rate this Entry
by , 11-27-2011 at 11:02 PM (5606 Views)
One of the most important changes in IT in the last 10 years has been the advent of Service Oriented Architecture (SOA). At the core SOA is the idea of building system via a common set of core services rather than the traditional monolithic method. Java and web services have been intimately intwined. There are a number of options are available for working with web services in Java. Spring facilitates the integration of many of these options. In fact, Spring provides support for publishing and consuming SOAP web services using the Java API for XML Web Services, or JAX-WS. In this entry, I will show you how to autowire JAX-WS Endpoints in Spring.

To use JAX-WS programming model, you rely heavily on annotations to declare a class and its methods as web service operations. A class thatís annotated with @WebService is considered a web service endpoint and its methods, annotated with @WebMethod, are the operations. JAX-WS endpoint will likely depend on other objects to do its work. This means that JAX-WS endpoints would benefit from dependency injection. But only if the endpointís lifecycle is managed by the JAX- WS runtime and not by Spring. The secret to working around this possible issue is of wiring JAX-WS endpoints is to extend SpringBeanAutowiringSupport. By extending SpringBeanAutowiringSupport, you can annotate an endpointís properties with @Autowired and its dependencies will be met. I give an example of this with the Springexample application.

Java Code:
import java.util.List;
import javax.jws.WebMethod; 
import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component;
import com.acme.springwebapp.domain.Car;
import com.acme.springwebapp.service.CarService;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

@Component
@WebService(serviceName ="CarService")
public class CarServiceEndpoint extends SpringBeanAutowiringSupport{
	@Autowired
	CarService carService;

	@WebMethod
	public void addCar(Car car) {
		carService.addCar(car);
	}

	@WebMethod
	public void deleteCar(Car car) {
		carService.deleteCar(car);
	}

	@WebMethod
	public List<Car> getCars(int carCount) {
		return carService.getCars(carCount);
	}
}
Weíve annotated the carService property with @Autowired to indicate that it should be automatically injected with a bean from the Spring application context. From there, this endpoint delegates to the injected carService to do the real work.
In the next tip, I will look at exporting Jax-ws endpoints.

Submit "Intro to Web Services with Spring" to Facebook Submit "Intro to Web Services with Spring" to Digg Submit "Intro to Web Services with Spring" to del.icio.us Submit "Intro to Web Services with Spring" to StumbleUpon Submit "Intro to Web Services with Spring" to Google

Updated 11-30-2011 at 01:35 PM by Spring Framework

Categories
Web , Web Service , Spring 3

Comments