In this tutorial we are going to
look at how to deploy a web service in Apache Tomcat web server. In all my
upcoming exercises I am going to use eclipse and apace tomcat (version 7 or
greater) as its being used widely for learning purpose. We can use our Hello
World example here.
- Create a web service
- Define WSServletContextListener and WSServlet in Web.xml (optional from Tomcat 7 onwards)
- Define sun-jaxws.xml in WEB-INF
- Add the necessary jar files in the classpath.
HelloWorld.java – Service Interface
package com.sri.ws;
import javax.jws.WebMethod;
import
javax.jws.WebService;
import
javax.jws.soap.SOAPBinding;
import
javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style = Style.DOCUMENT)
public interface HelloWorld{
@WebMethod
String
sayHelloToUser(String name);
}
HelloWorldImpl.java – Implementation Class
package com.sri.ws;
import
javax.jws.WebService;
//Service Implementation
@WebService(endpointInterface = "com.sri.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
@Override
public String sayHelloToUser(String
name) {
return "Hello Mr. " + name+" !!!";
}
}
Create a package named com.sri.ws
and drop the above two files in it.
Create sun-jaxws.xml and put in
under WEB-INF folder.
sun-jaxws.xml
<?xml version="1.0"
encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint name="HelloWorld"
implementation="com.sri.ws.HelloWorldImpl"
url-pattern="/hello"/>
</endpoints>
That’s all.
Your web service deployed in
Apache Tomcat (version 7.x and above).
You can access this service using
the URL.
Generic URL
http://localhost:8080/{your_web_application_name}/{url_pattern_defined_in_jax_rx_xml}
For Tomcat version 6.x and below you
need to define the WSServletContextListener and WSServlet servlets in the
WEB.XML.
<?xml version="1.0"
encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>JAXWS-DocumentStyle</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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
</web-app>
You need to add the following
jars in the java build path as well as deployment assembly in eclipse. We need
to add these jars to deployment assembly so that these jars will be available
to tomcat during runtime.
ha-api.jar
gmbal-api-only.jar
jaxb-impl.jar
jaxws-api.jar
jaxws-rt.jar
management-api.jar
policy.jar
stax-ex.jar
streambuffer.jar
jaxb-core.jar
activation.jar
mail.jar
activation.jar
mail.jar
No comments:
Post a Comment