Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Exposing JAX-WS Web Service Using Spring and Metro Implementation in Tomcat

DZone's Guide to

Exposing JAX-WS Web Service Using Spring and Metro Implementation in Tomcat

· Integration Zone ·
Free Resource

The new Gartner Critical Capabilities report explains how APIs and microservices enable digital leaders to deliver better B2B, open banking and mobile projects.

Here I will be creating a simple web service exposing customer web service. I am using metro implementation to publish the web service in tomcat.


Let’s start with the service endpoint implementation class.  Here I am using the @WebService annotation. 

@WebService(serviceName="customerService")
public class CustomerEndpoint implements CustomerService
{
private CustomerEndPointService service;

@WebMethod(exclude=true)
public void setService(CustomerManagementService service) {
this.service = service;
}

@Override
public Customer getCustomerById(String customerId)
{
Customer customer= service.getCustomerById(customerId);
return customer;
}


}

Spring context file (server.xml) exposing the customer endpoint using metro implementation. Here the customer endpoint is exposed with the address /customer

<bean id="customerService" class="com.sample.services.customers.CustomerServiceImpl">
</bean>

<bean id="customerEndpoint" class="com.sample.endpoint.CustomerEndpoint">
<property name="service" ref="customerService"/>   
</bean>

<wss:binding url="/customer">
<wss:service>
<ws:service bean="#customerEndpoint"/>
</wss:service>

</wss:binding>

Remember to include the schemaLocation and namespace

<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.xsd
http://jax-ws.dev.java.net/spring/corehttp://jax-ws.dev.java.net/spring/core.xsd
http://jax-ws.dev.java.net/spring/servlethttp://jax-ws.dev.java.net/spring/servlet.xsd">

Finally the web.xml file. Here we are using the WSSpringServlet to load the load the endpoint.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="2.5">

   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:server.xml</param-value>
   </context-param>
   <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>

Once this is deployed in a tomcat, you will be able to get the wsdl file by using the url:

http://localhost:8080/<Application_Context>/customer?wsdl

The new Gartner Critical Capabilities for Full Lifecycle API Management report shows how CA Technologies helps digital leaders with their B2B, open banking, and mobile initiatives. Get your copy from CA Technologies.

Topics:
java ,enterprise-integration ,spring ,tutorial ,jax-ws ,integration ,apache tomcat ,metro webservices

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}