php小编小新在使用spring 3.2时,发现了一个常见的问题——找不到@restcontroller注解。在使用@restcontroller注解时,可能会遇到编译错误或运行时错误,导致无法正常使用该注解。这个问题可能涉及到版本兼容性或配置问题,需要进行相应的解决和调整。在本文中,我们将探讨这个问题的原因和解决方法,帮助读者解决spring 3.2中找不到@restcontroller注解的困扰。
问题内容
这是有线的,我尝试设置@restcontroller,但它找不到它 从 intellij 社区 ide 运行 pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 3.2.2 com.restservice restservice 0.0.1-snapshot restservice restservice 17 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
和简单的类:
package com.restservice.restservice;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
@springbootapplication
public class restserviceapplication {
public static void main(string[] args) {
springapplication.run(restserviceapplication.class, args);
}
}
package com.restservice.restservice;
@restcontroller <--- can't find it painted with red
public class restcontroller {
}
还有编译错误:
[INFO] --- maven-compiler-plugin:3.11.0:compile (default-compile) @ restservice --- [INFO] Changes detected - recompiling the module! :source [INFO] Compiling 2 source files with javac [debug release 17] to target\classes [INFO] ------------------------------------------------------------- [ERROR] COMPILATION ERROR : [INFO] ------------------------------------------------------------- [ERROR] /C:/dev/my/java/spring_applications/ReactiveSpringBootREST Service2/restservice/src/main/java/com/restservice/restservice/RestController.java:[4,2] incompatible types: com.restservice .restservice.RestController cannot be converted to java.lang.annotation.Annotation [INFO] 1 error [INFO] ------------------------------------------------------------- [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.771 s [INFO] Finished at: 2024-02-01T17:45:04+02:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.11.0:compile (default-compile) on project restservice: Compilation failure [ERROR] /C:/dev/my/java/spring_applications/ReactiveSpringBootREST Service2/restservice/src/main/java/com/restservice/restservice/RestController.java:[4,2] incompatible types: com.restservice .restservice.RestController cannot be converted to java.lang.annotation.Annotation [ERROR] [ERROR] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException $ java -version java version "17.0.4.1" 2022-08-18 LTS Java(TM) SE Runtime Environment (build 17.0.4.1+1-LTS-2) Java HotSpot(TM) 64-Bit Server VM (build 17.0.4.1+1-LTS-2, mixed mode, sharing)
解决方法
您注释掉主要 Spring Boot 启动器依赖项有什么特殊原因吗?尝试使用 Maven 重新加载您的项目,并在项目窗口中检查您的外部库文件夹以确保您具有依赖项。 RestController注解预计从org.springframework.web.bind.annotation.RestController导入。










