Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey clashing with javax Servlet

I'm setting up a simple REST service with Jersey and Maven. For Jersey's versions greater than 1.8 the web.xml in IntelliJ throws this error:

'com.sun.jersey.spi.container.servlet.ServletContainer' is not assignable to 'javax.servlet.Servlet'

Does anyone knows how to fix this in order to use the latest version of Jersey?

This is the web.xml

<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" 
     version="2.5">

<display-name>R Proxy</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/r-proxy-log4j.properties</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Jersey configuration -->
<servlet>
    <servlet-name>jersey-servlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>org.fao.fenix.r.proxy.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<!-- Jersey configuration -->
<servlet-mapping>
    <servlet-name>jersey-servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

like image 950
Kalimaha Avatar asked Apr 16 '13 09:04

Kalimaha


2 Answers

For all who have the same issue - that's because you don't have servlet api in your classpath. Just add servlet-api dependency to your project and everything will be ok.

like image 150
user3601262 Avatar answered Nov 11 '22 19:11

user3601262


I was looking into this as well. Here is what I found, hopefully it can help others:

  1. You may have the wrong servlet class in your web.xml. For Jersey 2.0 and up, you should use the group "org.glassfish" instead of the old "com.sun.jersey". So, inside your web.xml, you should have:

    <servlet>
        ...
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    </servlet>
    
  2. I encountered this issue once as well when inadvertently using different versions of Jersey inside the same project. My project did import Jersey 1.9 bundle through Maven, but my code editor had a different version of Jersey on the project classpath. Clean up your classpath, reimport what you need and check your versions of Jersey, and it may solve your issue.

like image 21
Darkane Avatar answered Nov 11 '22 17:11

Darkane