Spring boot API with multiple controllers?

I am starting to learn Spring Boot. I'm struggling to find an example with several RestControllers that tells me that I can do something wrong. I am trying to make a very simple example: the goal is to make the following calls:

localhost:8080/ localhost:8080/employees/bob localhost:8080/departments 

I can only get localhost: 8080 / for display. Other calls return an answer: this application does not have an explicit mapping for / error, so you see this as a fallback.

 com.demo.departments Department.java DepartmentController.java com.demo.employees Employee.java EmployeeController.java com.demo BootDemoApplication.java 

the code:

 package com.demo.departments @RestController @RequestMapping("/departments") public class DepartmentController { @RequestMapping("") public String get(){ return "test.."; } @RequestMapping("/list") public List<Department> getDepartments(){ return null; } } -------------------------------------------------------------------- package com.demo.employees @RestController @RequestMapping("/employees") public class EmployeeController { Employee e =new Employee(); @RequestMapping(value = "/{name}", method = RequestMethod.GET, produces = "application/json") public Employee getEmployeeInJSON(@PathVariable String name) { e.setName(name); e.setEmail(" employee1@genuitec.com "); return e; } } ----------------------------------------------------------------------- package com.demo @RestController @SpringBootApplication public class BootDemoApplication { public static void main(String[] args) { SpringApplication.run(BootDemoApplication.class, args); } @RequestMapping("/") String home(){ return "<html> This is the home page for Boot Demo.</html>"; } 
+6
source share
7 answers

Apparently, controllers in different packages cannot be seen using the @springbootApplication notation in the main class. The solution described here is https://kamwo.me/java-spring-boot-mvc-ontroller-not-called/ .

+4
source

I tried Spring Boot and got the same problem, and just fixed it, I am posting my solution here because, in my opinion, this might be useful for someone.

First, put the application class (which contains the main method) in the root directory of the controllers:

 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 scans all components of the demo package subpackages

IndexController.java (return index.html )

 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 )

 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 log in. View indexes: http: // localhost: 8080 / demo / and Log in: http: // localhost: 8080 / demo / login

+5
source

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

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

This worked for me in a similar application using Spring-boot 1.4.0. For earlier versions of Spring-boot, it looks like you will refuse to use SpringBootApplication and instead use the following to get the same effect as above:

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

I found this in the comments on this post.

+1
source

Ensure that the @SpringBootApplication class is in a package that is above all other packages containing @RestControllers, or in the same package.

+1
source

Try the following: -

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

try it

 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
source

ComponentScan annotations work in most cases.

See below for an example, you can apply similar ones.
com.demo package

 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
source

All Articles