开发者

java.lang.ClassCastException: org.jersey.webservice.Login cannot be cast to javax.servlet.Servlet

开发者 https://www.devze.com 2023-04-07 20:02 出处:网络
I already done a lot of search and I can\'t fix this. I\'m bulding a Web Service with Tomcatv7.0, Jersey and Eclipse.

I already done a lot of search and I can't fix this.

I'm bulding a Web Service with Tomcatv7.0, Jersey and Eclipse.

This is the root cause:

java.lang.ClassCastException: org.jersey.webservice.Login cannot be cast to javax.servlet.Servlet org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) ...

This is the exception:

javax.servlet.ServletException: Class org.jersey.webservice.Login is not a Servlet org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) ...

I have a simple class:

package org.jersey.webservice;

import ...

@Path("/login")  public class Login {

    // This method is called if HTML is request
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHello() {
        return "<html> " + "<title>" + "Hello Andre" + "</title>"
                + "<body><h1>" + "Hello Andree" + "</body></h1>" + "</html> ";
    }
}

And this is my web.xml:

`<?xml version="1.0" encoding="UTF-8"?>
  <display-name>org.jersey.andre</display-name>
  <servlet>
    <servlet-name>Andre Jersey REST Service</serv开发者_Go百科let-name>
    <servlet-class>org.jersey.webservice.Login</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Andre Jersey REST Service</servlet-name>
    <url-pattern>/rest</url-pattern>
  </servlet-mapping>
</web-app>`

The Login class is in the package org.jersey.webservice and in WEB-INF/lib I´ve imported the needed jars (jersey-api, jersey-core, etc...).

Do you find anything wrong? I follow the documentation and this isn´t working. Damn!

Thanks in advance.


What tutorial were you reading? This is not the right way to declare a Jersey web service. It is indeed not directly a Servlet as the exception is trying to tell you. You need to declare the main Jersey servlet container with an init param pointing to the package containing the webservice classes.

<servlet>
    <servlet-name>Andre Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>org.jersey.webservice</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>Andre Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Also note that you should map it on the path /rest/* not the name /rest, otherwise you won't be able to use path information like http://example.com/context/rest/foo/bar and so on.

See also:

  • Jersey's own User Guide
  • Oracle's JAX-RS with Jersey tutorial (part of Java EE 6 tutorial)
  • Vogela's JAX-RS with Jersey tutorial

Unrelated to the concrete problem, consider choosing something else than org.jersey as main package. E.g. org.andreelias.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号