Home » Java » Enterprise Java » Web service application with JAX-WS and Spring

About Abhimanyu Prasad

Abhimanyu Prasad
Abhimanyu is a passionate tech blogger and senior programmer, who has an extensive end-to-end development experience with wide range of technologies. He is the founder and administrator at jCombat.

Web service application with JAX-WS and Spring

1. Introduction

It has been a long wait, but I am finally publishing a tutorial on creating the first SOAP based web service application with Spring. JAX-WS (Java API for XML Web Services) is a set of APIs for creating web services in XML format, which we also most commonly call as SOAP based web service, which hope we all are aware of the basic architecture.

2. Implementation

To start with, let’s check out the pom file configuration –

pom.xml

<!-- Spring dependencies -->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
	<version>4.2.1.RELEASE</version>
</dependency>
 
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-web</artifactId>
	<version>4.2.1.RELEASE</version>
</dependency>
 
<!-- JAX-WS -->
<dependency>
	<groupId>org.jvnet.jax-ws-commons.spring</groupId>
	<artifactId>jaxws-spring</artifactId>
	<version>1.9</version>
</dependency>
 
<dependency>
	<groupId>com.sun.xml.ws</groupId>
	<artifactId>jaxws-rt</artifactId>
	<version>2.2.8</version>
</dependency>

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
			http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">
 
    <display-name>SOAPWebServiceExample</display-name>
 
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <servlet>
        <servlet-name>customer</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>customer</servlet-name>
        <url-pattern>/customer</url-pattern>
    </servlet-mapping>
 
</web-app>

Let’s create the Customer entity for our application.

Customer.java

package com.jcombat.entity;
 
public class Customer {
	private int id;
	private String name;
 
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

Let’s now create the service interface and its corresponding service implementation class.

CustomerService.java

package com.jcombat.services.customers;
 
import com.jcombat.entity.Customer;
 
public interface CustomerService {
	public Customer getCustomerById(String customerId);
}

CustomerServiceImpl.java

package com.jcombat.services.customers;
 
import com.jcombat.entity.Customer;
 
public class CustomerServiceImpl implements CustomerService {
 
	public Customer getCustomerById(String customerId) {
		Customer customer = new Customer();
		customer.setId(123);
		customer.setName("Abhimanyu");
		return customer;
	}
}

Below is how the applicationContext should look like.

applicationContext.xml

<?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:ws="http://jax-ws.dev.java.net/spring/core"
	xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://jax-ws.dev.java.net/spring/core 
       http://jax-ws.java.net/spring/core.xsd
       http://jax-ws.dev.java.net/spring/servlet 
       http://jax-ws.java.net/spring/servlet.xsd">
 
	<bean id="customerService" class="com.jcombat.services.customers.CustomerServiceImpl">
	</bean>
 
	<bean id="customerEndpoint" class="com.jcombat.ws.CustomerEndpoint">
		<property name="service" ref="customerService" />
	</bean>
 
	<wss:binding url="/customer">
		<wss:service>
			<ws:service bean="#customerEndpoint" />
		</wss:service>
	</wss:binding>
 
</beans>

Note that a URL pattern (/customer) is bound to the web service endpoint implementation class (customerEndpoint), as can be seen in the above snippet. Below is how our customerEndpoint bean implementation class looks like.

CustomerEndpoint.java

package com.jcombat.ws;
 
import javax.jws.WebMethod;
import javax.jws.WebService;
 
import com.jcombat.entity.Customer;
import com.jcombat.services.customers.CustomerService;
 
@WebService(serviceName = "customerService")
public class CustomerEndpoint {
	private CustomerService service;
 
	@WebMethod(exclude = true)
	public void setService(CustomerService service) {
		this.service = service;
	}
 
	@WebMethod(operationName = "getCustomer")
	public Customer getCustomerById(String customerId) {
		Customer customer = service.getCustomerById(customerId);
		return customer;
	}
 
}

Note that @WebService annotation tells the server runtime environment to expose all the public methods of that class as web service methods. If we want to prevent any of the methods to be exposed as web service method, we need to annotation the method with @WebMethod(exclude = true), as can be seen in the above snippet. Similarly if we want to name the web service method to something different from the actual method name specified in the class (getCustomerById()), we need to add an operationName attribute to the @WebMethod annotation.

  • If you face any dependency issues while setting up the project, you can refer this link.

3. Running the application

  • http://localhost:8080/SOAPWebServiceExample/customer?wsdl

Once we hit the above URL, we can see the WSDL content displayed, as can be seen in the below snapshot.

wsdl

We can also test the endpoint with SOAP UI as well. Create the new SOAP project with the same WSDL location as mentioned above.

SOAP

4. Download the source code

Reference: Web service application with JAX-WS and Spring from our JCG partner Abhimanyu Prasad at the jCombat blog.
(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of