才開始學習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學習的動力。