我开始学习Spring Boot,但是我很难找到一个具有多个RestControllers示例的例子,这让我认为我的做法可能有问题。我正在尝试一个非常简单的例子:目标是使以下调用:
localhost:8080/
localhost:8080/employees/bob
localhost:8080/departments
我只能显示localhost:8080/。其他调用返回响应:此应用程序没有显式映射 /error,因此您看到这是一个回退。
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("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>";
}
http://localhost:8080/
提供任何控制器,所以错误可能在那里。 - g00glen00b