有没有办法知道 URL Spring 控制器要映射哪个完整的 URL?

Is there a way to know which full URL the Spring controller is going to map?

我有一个 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?

Is there a way to print to full url which this Spring controller is going to be mapped ?

您可以启用以下日志记录 属性:

log4j.category.org.springframework.web=INFO

现在,如果您重新启动 Tomcat(或任何服务器),那么在启动期间,当 Spring 容器初始化 DispactherServletHandlerMapping 时,它会将所有已识别的映射(来自各种 Controller 类)打印到日志中,以便您可以实际验证 Controller 类 将提供哪些映射。

P.S.: 这个记录只是为了list/log所有控制器映射,这将有助于debug/solve问题