IntelliJ IDEA中的代码标记有编译器错误,它在Eclipse中正常工作

时间:2015-07-08 18:33:15

标签: java eclipse spring servlets intellij-idea

我正在尝试设置我的IntelliJ工作区以在eclipse项目上进行开发。我遇到的一件事情令人困惑:

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

问题是以下类定义:

public class SomeClass extends MockHttpServletResponse {

问题似乎是因为MockHttpServletResponse将Collection<String> getHeaders(String)实现为public List getHeaders(String name)。在这里,我可以看到实现方法使用原始List,其中父级请求使用Collection键入的通用String。除了可能不是类型安全之外,为什么IntelliJ会将此标记为编译器错误而不是警告?

我无法更改任何这些库。我只是想在IntellJ 14中完成已经工作的东西,而没有Eclipse 4.3 +中的抱怨。

编辑:

我已经更新到IntelliJ 15.0,该项目现在使用Java 1.7而不是1.6。我仍在使用IntelliJ遇到这个问题,但问题并非在Eclipse中出现。我可以通过IntelliJ使用现有的Ant脚本编译项目,但我无法通过IDE进行调试。

这是我的班级定义

public class ExecutableServletResponse extends MockHttpServletResponse {
  ...

以下是“消息”窗格中显示的错误:

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

项目SDK使用的是1.7版本(准确地说是1.7.0_79)。语言级别为7.模块SDK和语言级别与项目匹配。

我已经尝试过使用eclipse编译器,但是应用程序仍然没有完全编译,并且无法运行,因为它无法编译此类,并且webapp的整个部分都无法编译为结果

以下是我的错误FWIW的屏幕截图:

submit an issue with the product team

1 个答案:

答案 0 :(得分:7)

您在类中看到错误,但真正的问题是Spring模拟库与您正在使用的Servlet规范不兼容。如果您升级到Servlet 3.0规范(或添加了一个以传递方式提取它的依赖项),则可能会发生这种情况。检查您的依赖项并确保:

  • 仅提供Servlet 2.5或
  • 您使用的是与Servlet 3.0兼容的Spring版本。还要确保所有Spring依赖项都使用相同的版本。

这种组合应该有效:

<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>

应该这样:

<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>

但这会失败:

<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>

它在Eclipse中工作但在IntelliJ中不工作这一事实表明您有多个提供相同类的依赖项。无法保证系统将使用哪个jar来加载类。这可能是因为您的类路径中同时包含servlet-apijavaee-web-api,或者因为类路径中同时包含spring-mockspring-test。在版本2.0.8之后,spring-mock中的类被移动到spring-test,只有版本3.1.0.RELEASE和更高版本的spring-test与Servlet 3.0兼容。