Home  >  Article  >  Spring 3.2 cannot find @RestController

Spring 3.2 cannot find @RestController

PHPz
PHPzforward
2024-02-08 22:57:08635browse

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete