4

I'm trying to setup my IntelliJ workspace to do development on an eclipse project. One of the things I've run into is rather confusing:

Error:(24, 8) java: SomeClass.java:24: getHeader(java.lang.String) in org.springframework.mock.web.MockHttpServletResponse cannot implement getHeader(java.lang.String) in javax.servlet.http.HttpServletResponse; attempting to use incompatible return type found : java.lang.Object required: java.lang.String 

The problem is the following class definition:

public class SomeClass extends MockHttpServletResponse { 

The problem seems to be because MockHttpServletResponse implements Collection<String> getHeaders(String) as public List getHeaders(String name). Here, I can see that the implementing method uses a raw List where the parent asks for a generic Collection typed with String. Aside from being potentially not type-safe, why would IntelliJ mark this as a complier error instead of a warning?

I have no option of changing any of these libraries. I'm simply trying to make work in IntellJ 14 what already works without complaints in Eclipse 4.3+.

EDIT:

I have since updated to IntelliJ 15.0, and the project is using Java 1.7 now instead of 1.6. I am still running into this issue with IntelliJ, but the issue is not presenting itself at all in Eclipse. I can compile the project using existing Ant scripts via IntelliJ, but I cannot debug through the IDE.

Here is my class definition

public class ExecutableServletResponse extends MockHttpServletResponse { ... 

Here is the error showing in my 'Messages' pane:

Error:(24, 8) java: getHeader(java.lang.String) in org.springframework.mock.web.MockHttpServletResponse cannot implement getHeader(java.lang.String) in javax.servlet.http.HttpServletResponse return type java.lang.Object is not compatible with java.lang.String 

The project SDK is using version 1.7 (1.7.0_79 to be exact). Language level is 7. Module SDK and Language Levels match the project.

I've tried using the eclipse compiler, but the app still doesn't fully compile, and will fail to run presumably because it fails to compile this class, and a whole part of the webapp doesn't compile as a result.

Here's a screenshot of my error, FWIW:

enter image description here

13
  • 1
    Are you using the same JDK in both cases? Commented Jul 8, 2015 at 18:37
  • 1
    And the same compliance level? Commented Jul 8, 2015 at 18:38
  • 1
    According to my project settings (using ctrl+alt+shift+s) shows that I'm using 1.6 as the project SDK and 1.6 as the language level (for project and all modules). This appears to be the same as I have in Eclipse (using jdk1.6.0_45 and 1.6 JDK complience) Commented Jul 8, 2015 at 19:18
  • Hmm...Spring Javadoc says that getHeaders correctly returns List<String>. Also, it's complaining about there being a method Object getHeader(String) rather than String getHeader(String), not about getHeaders. Did you override getHeader in your subclass? Commented Nov 5, 2015 at 19:50
  • 4
    Is it possible that your IntelliJ set-up is somehow using different servlet dependency? I just tried it and Spring 2.5.6 + servlet 3 produces this error, while Spring 2.5.6 with servlet 2.5 does not. There is an issue in Spring JIRA about incompatibility with Servlet 3, which was fixed with Spring 3.1. Commented Nov 5, 2015 at 20:36

1 Answer 1

7
+300

You are seeing the error in your class, but the real issue is that the Spring mock library is not compatible with the Servlet Specification you are using. This may happen if you upgraded to the Servlet 3.0 Spec (or added a dependency that pulled it in transitively). Check your dependencies and ensure that either:

  • Only Servlet 2.5 is provided, or
  • You are using a version of Spring that is compatible with Servlet 3.0. Also ensure that all of your Spring dependencies are using the same version.

This combination should work:

<dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-mock</artifactId> <version>2.0.8</version> </dependency> 

as should this:

<dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.1.0.RELEASE</version> <scope>test</scope> </dependency> 

But this will fail:

<dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.2.2.RELEASE</version> <scope>test</scope> </dependency> 

The fact that it works in Eclipse but not in IntelliJ suggests that you have multiple dependencies that provide the same classes. There is no guarantee as to which jar the system will use to load the class. This may either be because you have both servlet-api and javaee-web-api on your classpath or because you have both spring-mock and spring-test on your classpath. After version 2.0.8, the classes in spring-mock were moved to spring-test and only version 3.1.0.RELEASE and higher of spring-test are compatible with Servlet 3.0.

Sign up to request clarification or add additional context in comments.

1 Comment

It would appear that this is it. I removed spring-mock.jar from the IntelliJ classpath, and the error went away. It turns out that spring-test was also available, so that problem was resolved.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.