首頁 > Java > Spring 3.2找不到@RestController

Spring 3.2找不到@RestController

PHPz
發布: 2024-02-08 22:57:08
轉載
1067 人瀏覽過

php小編小新在使用Spring 3.2時,發現了一個常見的問題-找不到@RestController註解。使用@RestController註解時,可能會遇到編譯錯誤或執行時錯誤,導致無法正常使用該註解。這個問題可能涉及版本相容性或設定問題,需要進行相應的解決和調整。在本文中,我們將探討這個問題的原因和解決方法,幫助讀者解決Spring 3.2中找不到@RestController註解的困擾。

問題內容

這是有線的,我嘗試設定@restcontroller,但它找不到它 從 intellij 社區 ide 運行 pom.xml

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

<?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>

登入後複製

和簡單的類別:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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 {

 

}

登入後複製

還有編譯錯誤:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

[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中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板