php小编小新在使用Spring 3.2时,发现了一个常见的问题——找不到@RestController注解。在使用@RestController注解时,可能会遇到编译错误或运行时错误,导致无法正常使用该注解。这个问题可能涉及到版本兼容性或配置问题,需要进行相应的解决和调整。在本文中,我们将探讨这个问题的原因和解决方法,帮助读者解决Spring 3.2中找不到@RestController注解的困扰。
这是有线的,我尝试设置@restcontroller,但它找不到它 从 intellij 社区 ide 运行 pom.xml
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>3.2.2</version> <relativepath/> <!-- lookup parent from repository --> </parent> <groupid>com.restservice</groupid> <artifactid>restservice</artifactid> <version>0.0.1-snapshot</version> <name>restservice</name> <description>restservice</description> <properties> <java.version>17</java.version> </properties> <dependencies> <!--<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> </dependency>--> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project>
和简单的类:
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
导入。
以上是Spring 3.2找不到@RestController的详细内容。更多信息请关注PHP中文网其他相关文章!