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

时间:2016-11-07 06:31:18

标签: spring spring-mvc spring-web

我有一个Spring Controller,

@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?

1 个答案:

答案 0 :(得分:0)

  

有没有办法打印到这个Spring控制器的完整URL   要映射?

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

log4j.category.org.springframework.web=INFO

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

P.S。:此日志记录只是列出/记录所有控制器映射,这将有助于调试/解决问题