zoukankan      html  css  js  c++  java
  • springboot多模块controller访问的问题

    参考 https://blog.csdn.net/qq_25091649/article/details/88429512

    情况一:在主类直接写controller接口,能够访问到

    @SpringBootApplication
    @RestController
    public class DemoApplication {
    	@RequestMapping("/index")
        public String index() {
          
            return "this is index!";
        }
    
    
    	public static void main(String[] args) {
    		//SpringApplication.run(DemoApplication.class, args);
    		SpringApplication application = new SpringApplication(DemoApplication.class);
    		application.setBannerMode(Mode.OFF);//关闭banner
    		application.run(args);
    	}
    
    }
    


    情况二:controller类单独写,且controller所在目录低于主类目录,此时也可以访问到

      

    情况三:controller类单独写,且controller所在目录不在住类目录下,此时需要添加注解ComponentScan,里面写入要扫描的包

    @ComponentScan(basePackages = {"com.xiao","com.example"})
    @SpringBootApplication
    public class DemoApplication {
    
    	public static void main(String[] args) {
    		//SpringApplication.run(DemoApplication.class, args);
    		SpringApplication application = new SpringApplication(DemoApplication.class);
    		application.setBannerMode(Mode.OFF);//关闭banner
    		application.run(args);
    	}
    
    }
    

    1、当启动类和controller在同一类中时,需要在该类上添加注解@Controller;

    2、当启动类和controller分开时,启动目录高于controller目录,启动类上只有注解@SpringBootApplication;

    3、当启动类和controller分开时,如果启动类在某个包下,需要在启动类中增加注解@ComponentScan,配置需要扫描的包名;

    补充说明:对外的只有主类所在的模块,所以需要把其它模块添加到住类所在模块

  • 相关阅读:
    创建对象_原型(Prototype)模式_深拷贝
    创建对象_工厂方法(Factory Method)模式 与 静态工厂方法
    创建对象——单例(Singleton)模式
    模板方法模式
    移除HTML5 input在type="number"时的上下小箭头
    颜色名列表
    什么是盒模型?
    JQuery中$.ajax()方法参数详解
    zsh下docker命令tab补全方法
    ubuntu14.04 搭建gitlab服务
  • 原文地址:https://www.cnblogs.com/jthr/p/14230458.html
Copyright © 2011-2022 走看看