我開始學習Spring Boot。我正在努力尋找一個具有多個RestControllers的例子,這表明我可能做錯了什麼。我正在嘗試一個非常簡單的例子:目標是撥打電話,如下所示:帶有多個控制器的Spring Boot API?
localhost:8080/
localhost:8080/employees/bob
localhost:8080/departments
我只能得到localhost:8080 /來顯示。其他調用返回響應:此應用程序沒有顯式映射/錯誤,因此您將此視爲後備。
com.demo.departments
Department.java
DepartmentController.java
com.demo.employees
Employee.java
EmployeeController.java
com.demo
BootDemoApplication.java
代碼:
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>";
}
我不認爲你需要在頂級控制器映射上的前面的「/」。 – ChiefTwoPencils
我試過了,沒有什麼區別。 – user1529412
這應該工作(測試它)。儘管你沒有爲'http:// localhost:8080 /'提供任何控制器,所以也許錯誤在那裏。 – g00glen00b