spring boot用ide新建項目遇到的restcontroller不能導入的問題


才開始學習spring boot,第一個程序helloworld就碰到@RestController和@RequestMapping(/hello)的注解都會報錯的問題。 

我個人的解決方法:

1.springboot默認有

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
    </dependency>

</dependencies>
這時要引入Web模塊,需在pom.xml添加spring-boot-starter-web模塊

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

2. 然后用maven reimport 更新下依賴包。

3.Invalidate and restart 重啟下。

4.我使用intellj idea,這時重啟后的編輯器會自動提示要按alt+enter。如果不提示,就手動引入

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
應該就好了。  

希望新手不要被再調入這個坑了。嚴重打擊新手對java和spring學習的動力。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM