Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
I assume it to be a problem with the Application class import. Can you share the imports. You may be referring to an Application class which is not javax.ws.rs.core.Application; –  Juned Ahsan May 18 '13 at 12:19
    
following is the import of class import java.util.HashSet; import java.util.Set; import javax.ws.rs.core.Application; import com.resfultest.service.CustomerResources; . But i think, this is the problem of servlet , because when i call , the application not found any servlet. –  Harmeet Singh May 18 '13 at 12:31

3 Answers 3

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/

share|improve this answer

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.

share|improve this answer
    
how to send xml request using chrome restful client? –  Harmeet Singh May 19 '13 at 6:59
1  
That depends on what "chrome restful client" is, and this is unrelated and should be its own question. –  Ryan Stewart May 19 '13 at 14:46

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>
..................................
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.