title: Java CXF restful webservices
body: <p>My webservice class is as following:</p>
  
  <pre><code>package com.siemens.rest;
  import java.io.ByteArrayInputStream;
  import java.math.BigDecimal;
  
  import javax.annotation.Resource;
  import javax.servlet.ServletRequest;
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.transform.Source;
  import javax.xml.transform.dom.DOMSource;
  import javax.xml.transform.stream.StreamSource;
  import javax.xml.ws.BindingType;
  import javax.xml.ws.Provider;
  import javax.xml.ws.WebServiceContext;
  import javax.xml.ws.WebServiceProvider;
  import javax.xml.ws.handler.MessageContext;
  import javax.xml.ws.http.HTTPBinding;
  import javax.xml.ws.http.HTTPException;
  
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  import org.xml.sax.InputSource;
  
  @WebServiceProvider
  @BindingType(value = HTTPBinding.HTTP_BINDING)
  public class ConverterService implements Provider&lt;Source&gt; {
  
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
  
      @Resource
      protected WebServiceContext wsContext;
  
      private BigDecimal rupeeRate = new BigDecimal("40.58");
      private BigDecimal euroRate = new BigDecimal("0.018368");
  
      public Source invoke(Source source) {
          try {
              String amount = null;
  
              if (source == null) {
                  System.out.println("Getting input from query string");
                  MessageContext mc = wsContext.getMessageContext();
                  String query = (String) mc.get(MessageContext.QUERY_STRING);
                  System.out.println("Query String = " + query);
                  ServletRequest req = (ServletRequest) mc
                          .get(MessageContext.SERVLET_REQUEST);
                  amount = req.getParameter("amount");
              } else {
                  System.out.println("Getting input from input message");
                  Node n = null;
                  if (source instanceof DOMSource) {
                      n = ((DOMSource) source).getNode();
                  } else if (source instanceof StreamSource) {
                      StreamSource streamSource = (StreamSource) source;
                      DocumentBuilderFactory dbf = DocumentBuilderFactory
                              .newInstance();
                      DocumentBuilder db = dbf.newDocumentBuilder();
                      InputSource inputSource = null;
                      if (streamSource.getInputStream() != null) {
                          inputSource = new InputSource(
                                  streamSource.getInputStream());
                      } else if (streamSource.getReader() != null) {
                          inputSource = new InputSource(streamSource.getReader());
                      }
                      n = db.parse(inputSource);
                  } else {
                      throw new RuntimeException("Unsupported source: " + source);
                  }
                  NodeList children = n.getChildNodes();
                  for (int i = 0; i &lt; children.getLength(); i++) {
                      Node child = children.item(i);
                      if (child.getNodeName().equals("dollars")) {
                          amount = child.getAttributes().getNamedItem("amount")
                                  .getNodeValue();
                          break;
                      }
                  }
              }
              BigDecimal dollars = new BigDecimal(amount);
              BigDecimal rupees = dollarToRupees(dollars);
              BigDecimal euros = rupeesToEuro(rupees);
              return createResultSource(rupees, euros);
          } catch (Exception e) {
              e.printStackTrace();
              throw new HTTPException(500);
          }
      }
  
      public BigDecimal dollarToRupees(BigDecimal dollars) {
          BigDecimal result = dollars.multiply(rupeeRate);
          return result.setScale(2, BigDecimal.ROUND_UP);
      }
  
      public BigDecimal rupeesToEuro(BigDecimal rupees) {
          BigDecimal result = rupees.multiply(euroRate);
          return result.setScale(2, BigDecimal.ROUND_UP);
      }
  
      private Source createResultSource(BigDecimal rupees, BigDecimal euros) {
          String body = "&lt;ns:return xmlns:ns=\"http://rest.jaxws.samples.geronimo.apache.org\"&gt;"
                  + "&lt;ns:dollarToRupeesResponse&gt;"
                  + rupees
                  + "&lt;/ns:dollarToRupeesResponse&gt;&lt;ns:rupeesToEurosResponse&gt;"
                  + euros + "&lt;/ns:rupeesToEurosResponse&gt;&lt;/ns:return&gt;";
          Source source = new StreamSource(new ByteArrayInputStream(
                  body.getBytes()));
          return source;
      }
  }
  </code></pre>
  
  <p>and the web.xml as follows:</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_2_5.xsd"
    id="WebApp_ID" version="2.5"&gt;
    &lt;display-name&gt;jaxws-rest-converter&lt;/display-name&gt;
    &lt;servlet&gt;
      &lt;servlet-name&gt;ConverterService&lt;/servlet-name&gt;
      &lt;servlet-class&gt; com.siemens.rest.ConverterService &lt;/servlet-class&gt;
      &lt;load-on-startup&gt;0&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;ConverterService&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/converter&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>While deploying it in tomcat 6.0, iam getting the following exception:</p>
  
  <blockquote>
    <p>SEVERE: Servlet /jaxws-rest-converter threw load() exception
    java.lang.ClassCastException: com.siemens.rest.ConverterService cannot
    be cast to javax.servlet.Servlet  at
    org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1116)
      at
    org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993)
      at
    org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4420)
      at
    org.apache.catalina.core.StandardContext.start(StandardContext.java:4733)
      at
    org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
      at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
      at
    org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
      at
    org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
      at
    org.apache.catalina.core.StandardService.start(StandardService.java:525)
      at
    org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
      at org.apache.catalina.startup.Catalina.start(Catalina.java:595)    at
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   at
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)   at
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)   at
    java.lang.reflect.Method.invoke(Unknown Source)   at
    org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)   at
    org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)</p>
  </blockquote>
  
  <p>I think the <code>web.xml</code> is wrong, can anyone pls help me in this. Iam trying to implement restful webservices in <code>CXF</code>.</p>