Exposing JAX-WS Web Service Using Spring and Metro Implementation in Tomcat
Join the DZone community and get the full member experience.
Join For FreeHere 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
Opinions expressed by DZone contributors are their own.
Comments