[How to Solve] RestController cannot be recognized in spring boot

I just started to learn spring boot, the first program helloworld encountered the problem that the annotations of @RestController and @RequestMapping(/hello) would report errors.

My personal solution:

1. Springboot has by default

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

At this time, to introduce the web module, you need to add the spring-boot-starter-web module in pom.xml

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

 

2. Then use maven reimport to update the dependency package.

3. Invalidate and restart restart.

4. I use intellj idea, then the editor after restarting will automatically prompt to press alt+enter. If not prompted, manually import

import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;

It should be just fine.

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *