我有一个春天控制器,有没有办法知道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