【发布时间】:2018-03-18 17:50:09
【问题描述】:
我在 Eclipse 中有一个用于 Web 应用程序的简单 maven 项目。
我在 pom.xml 文件中添加了以下依赖项...
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
我创建了一个简单的网络控制器类...
import org.springframework.stereotype.Controller;
@Controller
public class WebController {
@RequestMapping(value="/method0")
@ResponseBody
public String method0(){
return "method0";
}
}
...项目可以找到@Controller注解很好,但是@RequestMapping和@ResponseBody注解出现红色错误。
我清理了一切,重建了一切,没有运气。
我检查了项目中的Java Build Path,路径中添加了“Maven Dependencies”,其中包含以下JAR ...
spring-context-5.0.4.RELEASE.jar
spring-aop-5.0.4.RELEASE.jar
spring-beans-5.0.4.RELEASE.jar
spring-core-5.0.4.RELEASE.jar
spring jcl-5.0.4.RELEASE.jar
spring-expression-5.0.4.RELEASE.jar
spring-webmvc-5.0.4.RELEASE.jar
spring-web-5.0.4.RELEASE.jar
另外,我的理解是 org.springframework.web.bind.annotation.RequestMapping 注释应该在 spring-webmvc-5.0.4.RELEASE.jar 中,但我解压缩了 JAR 文件,但在那里看不到它。
我做错了什么?
【问题讨论】:
-
您似乎只有一个导入。您需要其他导入。
-
谢谢,是的,这就是问题所在。项目找不到该名称的类/注释,因此导入语句将无效。
-
嗨——仅供参考,我有一个解决方案。我从我的 .m2 存储库中删除了 spring-web 和 spring-webmvc jar,并进行了 maven clean 和 maven 安装,它重新下载了 JAR 文件,现在 JAR 文件中有正确的文件。也许上次下载依赖项时出了点问题,很奇怪。但是现在工作所以我很好。
标签: spring eclipse maven spring-mvc spring-web