2016-11-07 79 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

相關問題