Heim  >  Artikel  >  Spring 3.2 kann @RestController nicht finden

Spring 3.2 kann @RestController nicht finden

PHPz
PHPznach vorne
2024-02-08 22:57:08636Durchsuche

PHP-Editor Xiaoxin hat ein häufiges Problem bei der Verwendung von Spring 3.2 entdeckt – die @RestController-Annotation konnte nicht gefunden werden. Bei Verwendung der @RestController-Annotation können Kompilierungs- oder Laufzeitfehler auftreten, die dazu führen, dass die Annotation nicht normal verwendet werden kann. Bei diesem Problem kann es sich um Versionskompatibilitäts- oder Konfigurationsprobleme handeln, die entsprechende Lösungen und Anpassungen erfordern. In diesem Artikel werden wir die Ursachen und Lösungen für dieses Problem diskutieren und den Lesern helfen, das Problem zu lösen, dass die @RestController-Annotation in Spring 3.2 nicht gefunden wird.

Frageninhalt

Das ist verkabelt und ich versuche, @restcontroller einzustellen, aber es kann es nicht finden Wird von der Intellij-Community-IDE ausgeführt 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>

und einfache Kurse:

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 {

}

Es gibt auch Kompilierungsfehler:

[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

Gibt es einen besonderen Grund, warum Sie die Hauptabhängigkeit des Spring Boot-Starters auskommentiert haben? Versuchen Sie, Ihr Projekt mit Maven neu zu laden und überprüfen Sie den Ordner Ihrer externen Bibliotheken im Projektfenster, um sicherzustellen, dass die Abhängigkeiten vorhanden sind. Es wird erwartet, dass RestController-Anmerkungen aus org.springframework.web.bind.annotation.RestController importiert werden.

Das obige ist der detaillierte Inhalt vonSpring 3.2 kann @RestController nicht finden. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:stackoverflow.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen