When php editor Xiaoxin used Spring 3.2, he discovered a common problem-the @RestController annotation could not be found. When using the @RestController annotation, you may encounter compilation errors or runtime errors, resulting in the inability to use the annotation normally. This problem may involve version compatibility or configuration issues, which require corresponding solutions and adjustments. In this article, we will explore the causes and solutions to this problem, and help readers solve the problem of not finding the @RestController annotation in Spring 3.2.
Question content
This is wired, I try to set @restcontroller but it can't find it Running from the intellij community 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>
and simple classes:
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 { }
There are also compilation errors:
[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)
Workaround
Is there any particular reason why you commented out the main Spring Boot starter dependency? Try reloading your project using Maven and check your external libraries folder in the project window to make sure you have the dependencies. RestController annotations are expected to be imported from org.springframework.web.bind.annotation.RestController
.
The above is the detailed content of Spring 3.2 cannot find @RestController. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
