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 very simple example: The goal is to make calls like the following:
localhost:8080/
localhost:8080/employees/bob
localhost:8080/departments
I can only get localhost:8080/ to display. The other calls return response: This application has no explicit mapping for /error, so you are seeing this as a fallback.
com.demo.departments
Department.java
DepartmentController.java
com.demo.employees
Employee.java
EmployeeController.java
com.demo
BootDemoApplication.java
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("[email protected]");
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>";
}
In Spring MVC, we can create multiple controllers at a time. It is required to map each controller class with @Controller annotation.
You cannot. A URL can only be mapped to a single controller. It has to be unique.
I have added 2 main classes with @SpringBootApplication.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With