Editor PHP Xiaoxin menemui masalah biasa apabila menggunakan Spring 3.2 - anotasi @RestController tidak dapat ditemui. Apabila menggunakan anotasi @RestController, anda mungkin menghadapi ralat kompilasi atau ralat masa jalan, mengakibatkan ketidakupayaan untuk menggunakan anotasi secara normal. Masalah ini mungkin melibatkan keserasian versi atau isu konfigurasi, yang memerlukan penyelesaian dan pelarasan yang sepadan. Dalam artikel ini, kami akan meneroka punca dan penyelesaian kepada masalah ini, dan membantu pembaca menyelesaikan masalah tidak menemui anotasi @RestController dalam Spring 3.2.
Ini berwayar dan saya cuba menetapkan @restcontroller tetapi ia tidak menemuinya Berjalan dari IDE komuniti intellij 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>
dan kelas mudah:
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 { }
Terdapat juga ralat kompilasi:
[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)
Adakah terdapat sebab istimewa mengapa anda mengulas pergantungan pemula Spring Boot utama? Cuba muat semula projek anda menggunakan Maven dan semak folder perpustakaan luaran anda dalam tetingkap projek untuk memastikan anda mempunyai kebergantungan. Anotasi RestController dijangka akan diimport daripada org.springframework.web.bind.annotation.RestController
.
Atas ialah kandungan terperinci Spring 3.2 tidak dapat mencari @RestController. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!