Web Service Host Example

Lets create a simple Hello World Web Service application.

Example

Web Service Host

This is the Web Service host application.
Their are 2 approaches to creating a Web Service: Contract First and Contract Last. We will first see Contract Last approach as it is easier one and does not require us to create WSDL or XSD which can be painful and time consuming.

A Web Service in Java can be made by help of many libraries ( API ) like Axis-2, Spring, JAX-WS. This blog uses jax-ws API to create the Web Service in contract last approach. So we do not have to create WSDL and worry about marshaling and marshaling of method parameters and response objects, JAX-WS takes care of all this. As I mentioned before that a Web Service is a web application in a nutshell it will have a web.xml file just like any other web application.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  id="WebApp_ID" version="2.5">
 
  <display-name>jaxwsExample</display-name>
 
  <listener>
    <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>helloWs</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>helloWs</servlet-name>
    <url-pattern>/HelloWs</url-pattern>
  </servlet-mapping>
  <session-config>
    <session-timeout>120</session-timeout>
  </session-config>
</web-app>

Above web.xml file defines a Listener and a Servlet. The Servlet WSServlet is the entry point of any request. The listener WSServletContextListener initializes the application and looks for sun-jaxws.xml which has the entry of the URL pattern and implementation class of the webservice.

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="HelloWs" implementation="webservicehost.webservicehost.HelloWs" url-pattern="/HelloWs" />
</endpoints>

In web.xml we defined a Servlet with url-pattern as /HelloWs. In sun-jaxws.xml we create and endpoint with the same url and provide the name and implementation class name of the Web Service we need to host. o now when any request comes for with url /HelloWs it will be redirected to the web method of class webservicehost.webservicehost.HelloWs.java


HelloWs.java

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style = Style.RPC)
public class HelloWs {
 
  public Return sayHello(String name) {
    if (name == null) {
      return new Return("Hello!");
    }
 
    return new Return("Hello! "+name);
  }
}


The above class HelloWs.java has two class level annotations @Webservice and @SOAPBinding.

@Webservice makes the class a webservice and the request and response parameters will be marshalled and unmarshalled automatically. @Webservice has attributes like name, portName, serviceName etc. But non of  this is mandatory.

@SOAPBinding has attribute 'style' which defines the encoding style of the messages sent to and fro from the Web Service. The encoding style are of 2 types 'DOCUMENT' and 'RPC'. They define the rules to be used when generating the WSDL file. We will not go in more details on this for now.


Return.java

public class Return {
    String code;

    public Return() {
        super();
    }

    public Return(String code) {
        super();
        this.code = code;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
    
}


This is a simple POJO class which is the return parameter of our HelloWs Web Service.
Thats it our Web Service is ready to be hosted. Create a war file of the application and deploy it on any Servlet Container like Tomcat, Jetty etc.

Now you will have to generate thr wsdl file and give it to the client so that the client can created th stub which will call the webservice. As this was a Contract Last approach you do not have to manually create the WSDL file. Open Browser and type the below URL
http://localhost:808/HelloWs
The response will be a xml file on the browser. View Source and save the file as hellows.wsdl

To download the source code of the application click here
To learn how to create a client for Web Service click the below link of Web Service Client.

Introduction >> Web Service Host >> Web Service Client
For Web Service Introduction refer this blog

Below are some posts that explain how to implement and test SOAP/REST Webservices

Host
SOAP REST
JAX-WS JAX-RS
Spring-ws Spring-MVC-REST
Client
SOAP REST
JAX-WS(wsimport) Google REST APP
SOAP UI Apache REST

Share the post