SpringBoot 中新建一个Controller类,提示从未使用过的解决方法

版权声明: https://blog.csdn.net/InternetProgrammers/article/details/89095855

不说废话看代码

新建了一个Controller类(HelloController)

package com.example.web;
@RestController
public class HelloController{

  @RequestMapping(value="/hello",method=RequestMethod.Get)
  public String hello(){
  
  		return  "hello world";
   }
}

这个是启动类

package com.example.demo;
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

Idea 提示HelloController 没有使用过。。。。

原因:启动类和controller的位置关系不对。。。。。
官方解释:Application(启动类)与 controller 属于同级。
解决办法:

package com.example.demo;
@SpringBootApplication
@ComponentScan(basePackages = "com.example")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

加上个@ComponentScan(basePackages=“com.example”) 问题解决。
将启动类和Controller 设置共同的父包,就OK了。

猜你喜欢

转载自blog.csdn.net/InternetProgrammers/article/details/89095855
今日推荐