title: WebServices Calculator Example using Java
body: <p>I have created an sample application of calculator for sum of two numbers using webservices in Java by using Jersey service.</p>
  
  <p>My code is : 
  Calculator.java</p>
  
  <pre><code>package p1;
  import javax.jws.WebService;
  import javax.jws.WebMethod;
  import javax.ws.rs.Path;
  
  @WebService
  @Path("/CalculatorService")
  public class Calculator
  {
      @WebMethod
      public String sum(int x,int y)
      {
          return ""+(x+y);
      }
  }
  </code></pre>
  
  <p>Calculator.java:</p>
  
  <pre><code>package mypack;
  
  import javax.jws.WebMethod;
  import javax.jws.WebParam;
  import javax.jws.WebResult;
  import javax.jws.WebService;
  import javax.xml.ws.RequestWrapper;
  import javax.xml.ws.ResponseWrapper;
  
  
  /**
   * This class was generated by the JAXWS SI.
   * JAX-WS RI 2.0_02-b08-fcs
   * Generated source version: 2.0
   * 
   */
  @WebService(name = "Calculator", targetNamespace = "http://p1/")
  public interface Calculator {
  
  
      /**
       * 
       * @param arg1
       * @param arg0
       * @return
       *     returns java.lang.String
       */
      @WebMethod
      @WebResult(targetNamespace = "")
      @RequestWrapper(localName = "sum", targetNamespace = "http://p1/", className = "mypack.Sum")
      @ResponseWrapper(localName = "sumResponse", targetNamespace = "http://p1/", className = "mypack.SumResponse")
      public String sum(
          @WebParam(name = "arg0", targetNamespace = "")
          int arg0,
          @WebParam(name = "arg1", targetNamespace = "")
          int arg1);
  
  }
  </code></pre>
  
  <p>CalculatorService.java:</p>
  
  <pre><code>package mypack;
  
  import java.net.MalformedURLException;
  import java.net.URL;
  import javax.xml.namespace.QName;
  import javax.xml.ws.Service;
  import javax.xml.ws.WebEndpoint;
  import javax.xml.ws.WebServiceClient;
  
  
  /**
   * This class was generated by the JAXWS SI.
   * JAX-WS RI 2.0_02-b08-fcs
   * Generated source version: 2.0
   * 
   */
  @WebServiceClient(name = "CalculatorService", targetNamespace = "http://p1/", wsdlLocation = "http://localhost:8081/MyCalculatorWs/CalculatorService?WSDL")
  public class CalculatorService
      extends Service
  {
  
      private final static URL CALCULATORSERVICE_WSDL_LOCATION;
  
      static {
          URL url = null;
          try {
              url = new URL("http://localhost:8081/MyCalculatorWs/CalculatorService?WSDL");
          } catch (MalformedURLException e) {
              e.printStackTrace();
          }
          CALCULATORSERVICE_WSDL_LOCATION = url;
      }
  
      public CalculatorService(URL wsdlLocation, QName serviceName) {
          super(wsdlLocation, serviceName);
      }
  
      public CalculatorService() {
          super(CALCULATORSERVICE_WSDL_LOCATION, new QName("http://p1/", "CalculatorService"));
      }
  
      /**
       * 
       * @return
       *     returns Calculator
       */
      @WebEndpoint(name = "CalculatorPort")
      public Calculator getCalculatorPort() {
          return (Calculator)super.getPort(new QName("http://p1/", "CalculatorPort"), Calculator.class);
      }
  
  }
  </code></pre>
  
  <p>ObjectFactory.java:</p>
  
  <pre><code>package mypack;
  
  import javax.xml.bind.JAXBElement;
  import javax.xml.bind.annotation.XmlElementDecl;
  import javax.xml.bind.annotation.XmlRegistry;
  import javax.xml.namespace.QName;
  
  
  /**
   * This object contains factory methods for each 
   * Java content interface and Java element interface 
   * generated in the mypack package. 
   * &lt;p&gt;An ObjectFactory allows you to programatically 
   * construct new instances of the Java representation 
   * for XML content. The Java representation of XML 
   * content can consist of schema derived interfaces 
   * and classes representing the binding of schema 
   * type definitions, element declarations and model 
   * groups.  Factory methods for each of these are 
   * provided in this class.
   * 
   */
  @XmlRegistry
  public class ObjectFactory {
  
      private final static QName _Sum_QNAME = new QName("http://p1/", "sum");
      private final static QName _SumResponse_QNAME = new QName("http://p1/", "sumResponse");
  
      /**
       * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: mypack
       * 
       */
      public ObjectFactory() {
      }
  
      /**
       * Create an instance of {@link Sum }
       * 
       */
      public Sum createSum() {
          return new Sum();
      }
  
      /**
       * Create an instance of {@link SumResponse }
       * 
       */
      public SumResponse createSumResponse() {
          return new SumResponse();
      }
  
      /**
       * Create an instance of {@link JAXBElement }{@code &lt;}{@link Sum }{@code &gt;}}
       * 
       */
      @XmlElementDecl(namespace = "http://p1/", name = "sum")
      public JAXBElement&lt;Sum&gt; createSum(Sum value) {
          return new JAXBElement&lt;Sum&gt;(_Sum_QNAME, Sum.class, null, value);
      }
  
      /**
       * Create an instance of {@link JAXBElement }{@code &lt;}{@link SumResponse }{@code &gt;}}
       * 
       */
      @XmlElementDecl(namespace = "http://p1/", name = "sumResponse")
      public JAXBElement&lt;SumResponse&gt; createSumResponse(SumResponse value) {
          return new JAXBElement&lt;SumResponse&gt;(_SumResponse_QNAME, SumResponse.class, null, value);
      }
  
  }
  </code></pre>
  
  <p>package-info.java:</p>
  
  <pre><code>@javax.xml.bind.annotation.XmlSchema(namespace = "http://p1/")
  package mypack;
  </code></pre>
  
  <p>Sum.java:</p>
  
  <pre><code>package mypack;
  
  import javax.xml.bind.annotation.XmlAccessType;
  import javax.xml.bind.annotation.XmlAccessorType;
  import javax.xml.bind.annotation.XmlType;
  
  
  /**
   * &lt;p&gt;Java class for sum complex type.
   * 
   * &lt;p&gt;The following schema fragment specifies the expected content contained within this class.
   * 
   * &lt;pre&gt;
   * &amp;lt;complexType name="sum"&gt;
   *   &amp;lt;complexContent&gt;
   *     &amp;lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
   *       &amp;lt;sequence&gt;
   *         &amp;lt;element name="arg0" type="{http://www.w3.org/2001/XMLSchema}int"/&gt;
   *         &amp;lt;element name="arg1" type="{http://www.w3.org/2001/XMLSchema}int"/&gt;
   *       &amp;lt;/sequence&gt;
   *     &amp;lt;/restriction&gt;
   *   &amp;lt;/complexContent&gt;
   * &amp;lt;/complexType&gt;
   * &lt;/pre&gt;
   * 
   * 
   */
  @XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "sum", propOrder = {
      "arg0",
      "arg1"
  })
  public class Sum {
  
      protected int arg0;
      protected int arg1;
  
      /**
       * Gets the value of the arg0 property.
       * 
       */
      public int getArg0() {
          return arg0;
      }
  
      /**
       * Sets the value of the arg0 property.
       * 
       */
      public void setArg0(int value) {
          this.arg0 = value;
      }
  
      /**
       * Gets the value of the arg1 property.
       * 
       */
      public int getArg1() {
          return arg1;
      }
  
      /**
       * Sets the value of the arg1 property.
       * 
       */
      public void setArg1(int value) {
          this.arg1 = value;
      }
  
  }
  </code></pre>
  
  <p>SumResponse.java:</p>
  
  <pre><code>package mypack;
  
  import javax.xml.bind.annotation.XmlAccessType;
  import javax.xml.bind.annotation.XmlAccessorType;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlType;
  
  
  /**
   * &lt;p&gt;Java class for sumResponse complex type.
   * 
   * &lt;p&gt;The following schema fragment specifies the expected content contained within this class.
   * 
   * &lt;pre&gt;
   * &amp;lt;complexType name="sumResponse"&gt;
   *   &amp;lt;complexContent&gt;
   *     &amp;lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"&gt;
   *       &amp;lt;sequence&gt;
   *         &amp;lt;element name="return" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/&gt;
   *       &amp;lt;/sequence&gt;
   *     &amp;lt;/restriction&gt;
   *   &amp;lt;/complexContent&gt;
   * &amp;lt;/complexType&gt;
   * &lt;/pre&gt;
   * 
   * 
   */
  @XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "sumResponse", propOrder = {
      "_return"
  })
  public class SumResponse {
  
      @XmlElement(name = "return")
      protected String _return;
  
      /**
       * Gets the value of the return property.
       * 
       * @return
       *     possible object is
       *     {@link String }
       *     
       */
      public String getReturn() {
          return _return;
      }
  
      /**
       * Sets the value of the return property.
       * 
       * @param value
       *     allowed object is
       *     {@link String }
       *     
       */
      public void setReturn(String value) {
          this._return = value;
      }
  
  }
  </code></pre>
  
  <p>And my web.xml file is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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_3_0.xsd" id="WebApp_ID" version="3.0"&gt;
    &lt;display-name&gt;CalculatorEx&lt;/display-name&gt;
    &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.html&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;
    &lt;/welcome-file-list&gt;
   &lt;servlet&gt;
      &lt;servlet-name&gt;Jersey REST Service&lt;/servlet-name&gt;
      &lt;servlet-class&gt;com.sun.jersey.spi.container.servlet.ServletContainer&lt;/servlet-class&gt;
      &lt;init-param&gt;
        &lt;param-name&gt;com.sun.jersey.config.property.packages&lt;/param-name&gt;
        &lt;param-value&gt;com.restful.demo.resources&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Jersey REST Service&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>I am executing my application using:
  <code>http://localhost:8080/CalculatorEx/rest/CalculatorService</code></p>
  
  <p>i am getting the error like this:</p>
  
  <pre><code>exception:
  javax.servlet.ServletException: Servlet.init() for servlet Jersey REST Service threw exception
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
      org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
      org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
      org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
      org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
      java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
      java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
      java.lang.Thread.run(Thread.java:679)
  
  root cause:
  com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
      com.sun.jersey.server.impl.application.RootResourceUriRules.&lt;init&gt;(RootResourceUriRules.java:99)
      com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1298)
      com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:169)
      com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:775)
      com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:771)
      com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
      com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:771)
      com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:766)
      com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:488)
      com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:318)
      com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
      com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
      com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373)
      com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556)
      javax.servlet.GenericServlet.init(GenericServlet.java:160)
      org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
      org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
      org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
      org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
      org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
      org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
      org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
      java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
      java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
      java.lang.Thread.run(Thread.java:679)
  </code></pre>