【发布时间】:2016-11-07 06:31:18
【问题描述】:
我有一个 Spring 控制器,
@Controller
@RequestMapping(value = "/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
/**
* returns all the employees in the datastore.
*
* @return list of all employees.
*/
@RequestMapping(method = RequestMethod.GET)
public @ResponseBody String getAllEmployees() {
return convertObjectListToJSONArray(this.employeeService.listEmployees());
}
/**
* adds an employee in the data-store.
*
* @param employee
* employee to add in the data-store.
* @return request status.
*/
@ResponseStatus(value = HttpStatus.CREATED)
@RequestMapping(method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
public ResponseEntity<?> addEmployee(UriComponentsBuilder uriCBuilder, @RequestBody Employee employee) {
Employee e = this.employeeService.getEmployeeById(employee.getId());
if(null != e) {
throw new EmployeeConflictException();
}
this.employeeService.addEmployee(employee);
UriComponents uriComponents = uriCBuilder.path("/cmpe281Pratik021/rest/employee/{id}")
.buildAndExpand(employee.getId());
HttpHeaders headers = new HttpHeaders();
headers.setLocation(uriComponents.toUri());
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}
但是,当我点击 http://localhost:8080/employee 时,它会给出错误 404。 有没有办法打印到这个 Spring 控制器将被映射的完整 url?
【问题讨论】:
-
您点击的 URL 中似乎缺少您的上下文。 localhost:8080/<Context-Name>/employee
-
您是如何部署此应用程序的?它是一个 .war 文件吗?还是春季启动?如果是 .war,您还需要 contextPath(.war 的名称)。
http://localhost:8080/<name of war>/employee
标签: spring spring-mvc spring-web