Harmeet Singh Taara
Harmeet Singh Taara

Reputation: 6611

Java : Deploy restful web service without jersey

Thanks in advance. I am new in web services , when i try to deploy the restful web service using java , it will generate an exception. I am follow the RESTful Java with JAX-RS to learn web services, according to this book chapter 3 ,i try to build the example , after deploying the example it will generate the exception , i know , i miss something , but i don't know what i missing. following is my code of example .

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>RestfulTest</display-name>
<servlet>
<servlet-name>Rest</servlet-name>
<servlet-class>com.restfultest.controller.ShoppingApplication</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Rest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

Following is the class

 public class ShoppingApplication extends Application {

private Set<Object> singletons = new HashSet<Object>();
private Set<Class<?>> empty = new HashSet<Class<?>>();
public ShoppingApplication() {
    singletons.add(new CustomerResources());
}

@Override
public Set<Class<?>> getClasses() {
    return empty;
}

@Override
public Set<Object> getSingletons() {
    return singletons;
}
  }

when try to deploy the application following the exception occur.

 java.lang.ClassCastException: com.restfultest.controller.ShoppingApplication cannot be cast to javax.servlet.Servlet
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)

this is silly question i know , ShoppingApplication is not the servlet class but accordin to this book ,it is servlet class. to call this services , i use chrome restful client. Please suggest me how to use chrome restful client and how to send XML or JSON request using this client.

Upvotes: 1

Views: 4262

Answers (3)

Harmeet Singh Taara
Harmeet Singh Taara

Reputation: 6611

This is the problem occur because the Tomcat Container does not aware about JAX-RS . So to define the implementation of JAX-RS we need to add third party libraries . When i add the Jersey lib and add the Jersey servlet definition in container , my example run successfully. Following is the `web.xml

...............................
<servlet>
<servlet-name>Rest</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
  <init-param>
    <param-name>javax.ws.rs.Application</param-name>
    <param-value>com.restfultest.controller.ShoppingApplication</param-value>
  </init-param>
</servlet>

<servlet-mapping>
<servlet-name>Rest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
..................................

Upvotes: 1

Ryan Stewart
Ryan Stewart

Reputation: 128919

You should have

<servlet-name>com.restfultest.controller.ShoppingApplication</servlet-name>

and no servlet-class. The Jersey project has a good reference for the various options of deploying JAX-RS 1.1 applications, including this specific case.

Also note that this only works in Servlet 3.0. In earlier servlet specs, you have to provide an actual servlet class that implements a JAX-RS container, like Jersey's ServletContainer.

Upvotes: 1

fGo
fGo

Reputation: 1146

The problem is that you are telling that your class is a servlet which is not true. Thre must be a problem in the documentation you are following. Take a look at this http://www.ibm.com/developerworks/library/wa-aj-tomcat/

Upvotes: 1

Related Questions