2016-11-07 75 views
0

我有一个春天控制器,有没有办法知道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 有没有办法打印到今年春季控制器将被映射完整网址?

+0

看来您的上下文中缺少你打的URL。 http:// localhost:8080/ /员工 – Khuzi

+1

您是如何部署此应用程序的?它是一个.war文件吗?还是Spring Boot?如果.war,你也需要contextPath(.war的名字)。 'http:// localhost:8080/ /员工' – shazin

回答

0

有没有办法打印到完整的URL,这个春天控制器是 将被映射?

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

log4j.category.org.springframework.web=INFO 

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

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

+0

我要试试这个。希望它能解决问题。 – pratiksanglikar

+0

它会帮助你列出所有的映射,这将有助于解决问题 – developer

+0

如何启用该属性? – pratiksanglikar

相关问题