我有一個春天控制器,有沒有辦法知道Spring控制器要映射哪個完整的URL?
@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 有沒有辦法打印到今年春季控制器將被映射完整網址?
看來您的上下文中缺少你打的URL。 http:// localhost:8080/ /員工 –
Khuzi
您是如何部署此應用程序的?它是一個.war文件嗎?還是Spring Boot?如果.war,你也需要contextPath(.war的名字)。 'http:// localhost:8080/ /員工' –
shazin