JAX-WS webservice deployment example in Tomcat Server

Introduction

We will create a post on JAX-WS webservice deployment example in Tomcat server. We have seen that we have successfully created the jax-ws webservice example tutorial and also published and tested the service successfully. Now we will deploy the same webservice in Tomcat Server.

Prerequisites

Eclipse 4.12, JDK 12 or 8, Tomcat 9.0.24, JAX-WS API, Gradle 5.6

Creating Project

Create a gradle based project in Eclipse. The project name is jax-ws-webservice-tomcat.

Updating Build File

We need to update the build file for including required dependencies in build.gradle file.

plugins {
    id 'java-library'
    id 'war'
}
sourceCompatibility = 12
targetCompatibility = 12
repositories {
    mavenCentral()
	maven {
		url 'http://maven.java.net/content/repositories/staging/'
	}
}
dependencies {
    implementation 'javax.xml.ws:jaxws-api:2.3.1'
    implementation 'com.sun.xml.ws:jaxws-rt:2.3.1'
    implementation 'org.glassfish.jaxb:txw2:2.4.0-b180608.0325'
}

Creating End-point Interface

Create an interface that will act as an endpointInterface.

package com.roytuts.jax.ws.service;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style = Style.RPC)
public interface Hello {
	@WebMethod
	public String sayHello(String name);
}

End-point Interface Implementation

endpointInterface attribute is used to separate between the implementing class and the interface. Basically, this determines what will be mapped to wsdl:portType when the service is deployed and the wsdl:definitionis generated.

If you do not define the endpointInterface all public methods of the annotated class will be mapped to wsdl:operation.

If you define the endpointInterface, it has to point to some type which the annotated class implements. Then, the public methods of this type are used for mapping the wsdl:portType, instead of the methods of the annotated class.

package com.roytuts.jax.ws.service.impl;
import javax.jws.WebService;
import com.roytuts.jax.ws.service.Hello;
@WebService(endpointInterface = "com.roytuts.jax.ws.service.Hello")
public class HelloImpl implements Hello {
	@Override
	public String sayHello(String name) {
		return "Hello " + name;
	}
}

Deployment Descriptor

As we are going to deploy our SOAP based webservice into Tomcat server. Therefore we need deployment descriptor file – web.xml – under WEB-INF folder.

Add JAX-WS webservice listener class.

<?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_1.xsd"
	version="3.1">
	<display-name>JAX-WS Web Service Tomcat Deployment</display-name>
	<listener>
		<listener-class>
			com.sun.xml.ws.transport.http.servlet.WSServletContextListener
		</listener-class>
	</listener>
	<servlet>
		<servlet-name>Hello</servlet-name>
		<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet
		</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>Hello</servlet-name>
		<url-pattern>/Hello</url-pattern>
	</servlet-mapping>
</web-app>

Configuring JAX-WS Endpoint

Create another sun-jaxws.xml file and put it under WEB-INF directory. This file is used to recognize the JAX-WS endpoint service.

<?xml version="1.0" encoding="UTF-8"?>
<endpoints
	xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
	<endpoint name="Hello"
		implementation="com.roytuts.jax.ws.service.impl.HelloImpl"
		url-pattern="/hello" />
</endpoints>

Deploying Application

Now deploy the war file into Tomcat Server. Put the war file under tomcat’s webapp directory. Run the Tomcat server by executing startup.bat file from command line tool.

Accessing WSDL File

To access the WSDL directly use URL http://localhost:8080/jax-ws-webservice-tomcat/hello?wsdl in the browser.

<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.service.ws.jax.roytuts.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://impl.service.ws.jax.roytuts.com/" name="HelloImplService">
<import namespace="http://service.ws.jax.roytuts.com/" location="http://localhost:8080/jax-ws-webservice-tomcat/hello?wsdl=1"/>
	<binding xmlns:ns1="http://service.ws.jax.roytuts.com/" name="HelloImplPortBinding" type="ns1:Hello">
		<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
		<operation name="sayHello">
			<soap:operation soapAction=""/>
			<input>
				<soap:body use="literal" namespace="http://service.ws.jax.roytuts.com/"/>
			</input>
			<output>
				<soap:body use="literal" namespace="http://service.ws.jax.roytuts.com/"/>
			</output>
		</operation>
	</binding>
		<service name="HelloImplService">
		<port name="HelloImplPort" binding="tns:HelloImplPortBinding">
			<soap:address location="http://localhost:8080/jax-ws-webservice-tomcat/hello"/>
		</port>
	</service>
</definitions>

Testing the Application

We will see how to test the service after generating client/stub from the WSDL file in another tutorial.

Source Code

Download Source Code

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *