Jersey clashing with javax Servlet
Asked Answered
M

3

6

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>

Methodius answered 16/4, 2013 at 9:40 Comment(2)
Did you ever figure out what this was? I'm getting this, too.Ecto
I think this #10009699 helps you.Alkalize
S
3

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.

Stegman answered 14/7, 2015 at 11:52 Comment(1)
What's servlet-api? Whats the groupId? Giving the full maven/gradle dependency code would be helpful.Ethyl
D
1

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.

Deason answered 28/5, 2014 at 18:3 Comment(0)
R
0

The error corresponds to which you must add the dependencies that are occupied by com.sun.jersey.spi.container.servlet.ServletContainer

enter image description here

Ragouzis answered 1/8, 2016 at 0:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.