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

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

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

Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

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

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}