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

age*_*154 4 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
Run Code Online (Sandbox Code Playgroud)

问题是以下类定义:

public class SomeClass extends MockHttpServletResponse {
Run Code Online (Sandbox Code Playgroud)

问题似乎是因为MockHttpServletResponse实现Collection<String> getHeaders(String)public List getHeaders(String name).在这里,我可以看到实现方法使用raw 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 {
  ...
Run Code Online (Sandbox Code Playgroud)

以下是我的"消息"窗格中显示的错误:

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
Run Code Online (Sandbox Code Playgroud)

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

我已经尝试过使用eclipse编译器,但是app仍然没有完全编译,并且无法运行,因为它无法编译这个类,并且webapp的整个部分都不会因此而编译.

这是我的错误FWIW的屏幕截图:

在此输入图像描述

Car*_*aet 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>
Run Code Online (Sandbox Code Playgroud)

应该这样:

<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>
Run Code Online (Sandbox Code Playgroud)

但这会失败:

<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>
Run Code Online (Sandbox Code Playgroud)

它在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兼容.