Spring Boot API with Multiple Controllers?

后端 未结 8 1057
孤城傲影
孤城傲影 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:04

    For Spring-boot 1.3.x and up, passing a base package to SpringBootApplication should work:

    @SpringBootApplication(scanBasePackages = {"com.demo"})
    public class DemoBootApplication {
        // code
    }
    

    This worked for me on a similar application using spring-boot 1.4.0. For earlier versions of spring-boot, it appears you'll have forego using SpringBootApplication and instead use the following to get same effect as above:

    @Configuration
    @EnableAutoConfiguration
    @ComponentScan(basePackages = {"com.demo"})
    public class DemoBootApplication {
        // code
    }
    

    I found this in the comments on this blog post.

    0 讨论(0)
  • 2021-02-07 13:04

    Try below:-

    @ComponentScan
    @Configuration
    @EnableAutoConfiguration
    public class BootDemoApplication {
    
    public static void main(String[] args) {
    
        SpringApplication.run(BootDemoApplication.class);
    }
    }
    
    @RestController
    @RequestMapping(value = "test", produces =    MediaType.APPLICATION_JSON_VALUE)
    public class TestController {
    
    @RequestMapping(method = RequestMethod.GET)
    public String test() {
        return "from test method";
    }
    
    }
    
    0 讨论(0)
  • 2021-02-07 13:08

    Apparently Controllers in different packages can't be seen with @springbootApplication notation in the main class. The solution explained here, https://kamwo.me/java-spring-boot-mvc-ontroller-not-called/.

    0 讨论(0)
  • 2021-02-07 13:09

    I'm not sure if this is the right way to do it, but when I changed my 2nd Controllers annotation from @Controller to @RestController it started working.

    0 讨论(0)
  • 2021-02-07 13:13

    I'm trying Spring Boot and got same problem, and just fixed it, I post my solution here because I think it maybe helpful for someone.

    First, put application class ( which contain main method) at the root of controllers's package:

    com.example.demo
                  |
                  +-> controller
                  |      |
                  |      +--> IndexController.java
                  |      +--> LoginController.java
                  |
                  +-> Application.java
    

    Application.java

    package com.example.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    Spring will scan all the components of sub-packages of demo package

    IndexController.java (return index.html view)

    package com.example.demo.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    @RequestMapping(value = {""})
    public class IndexController {
    
        @GetMapping(value = {""})
        public ModelAndView index() {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("index");
            return modelAndView;
        }
    
    }
    

    LoginController.java (return login.html view)

    package com.example.demo.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    @RequestMapping(value = {"/login"})
    public class LoginController {
        @GetMapping(value = {""})
        public ModelAndView login() {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("login");
            return modelAndView;
        }
    }
    

    And now I can enter Index view : http://localhost:8080/demo/ and Login view : http://localhost:8080/demo/login

    0 讨论(0)
  • 2021-02-07 13:18

    Make sure that the @SpringBootApplication class is in a package which is a level above all other packages that contain @RestControllers, or in the same package.

    0 讨论(0)
提交回复
热议问题