Spring Boot API with Multiple Controllers?

后端 未结 8 1058
孤城傲影
孤城傲影 2021-02-07 12:36

I am starting to learn Spring Boot. I am struggling to find an example with multiple RestControllers, which indicates to me that I may be doing something wrong. I am trying a ve

相关标签:
8条回答
  • 2021-02-07 13:22

    ComponentScan annotation works in most cases.

    See below example, you could apply similar.
    package com.demo;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @ComponentScan(basePackages = {"com.demo"})
    @SpringBootApplication
    public class DemoApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
    }
    
    0 讨论(0)
  • 2021-02-07 13:26

    Try this

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Main {
    
        public static void main(String[] args) {
    
            Object[] sources = new Object[2];
            sources[0] = Controller1.class;
            sources[1] = Controller2.class;
            SpringApplication.run(sources, args);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题