2016-05-12 41 views
2

我有一個控制器,它實現了ResourceProcessor<RepositorySearchesResource>,它包含下面的請求映射並覆蓋process方法爲這個請求映射創建一個搜索URI。我這樣做的方式似乎非常脆弱,因爲我在uri中將參數名稱指定爲字符串,我還將該路徑指定爲字符串。使用Spring HATEOAS構建一個模板化的搜索資源uri

理想情況下,我正在尋找一些方法,可以使用我的請求映射中定義的參數名稱來構建搜索uri,這樣,如果我更改它們,我不必更改搜索uri。最後,我想避免在uri中將路徑指定爲字符串,所以我不確定是否可以基於請求映射方法名稱或其他方式動態構建它。

此外,我想避免建立PageableTemplateVariable以及。

@RequestMapping(method = RequestMethod.GET, value = "/orders/search/exceptionsByDate") 
public @ResponseBody ResponseEntity<?> getAllExceptionsByDate(Pageable pageable, @RequestParam BigDecimal earlyPickupDate, @RequestParam List<String> status, @RequestParam String costCenter) { 
    Page<OrderExceptionProjection> exceptions = orderService.getExceptions(pageable, earlyPickupDate, status, costCenter); 
    return ResponseEntity.ok(new Resources<>(exceptions)); 
} 

@Override 
public RepositorySearchesResource process(RepositorySearchesResource resource) { 
    TemplateVariable earlyPickupDate = new TemplateVariable("earlyPickupeDate", TemplateVariable.VariableType.REQUEST_PARAM, "Selects all records with earlyPickupDate >= to the value specified."); 
    TemplateVariable status = new TemplateVariable("status", TemplateVariable.VariableType.REQUEST_PARAM_CONTINUED, "Specifies the order status."); 
    TemplateVariable costCenter = new TemplateVariable("costCenter", TemplateVariable.VariableType.REQUEST_PARAM_CONTINUED, "Specified the cost center to return orders for."); 
    TemplateVariables vars = new TemplateVariables(earlyPickupDate, status, costCenter); 
    UriTemplate uri = new UriTemplate(resource.getId().getHref() + "exceptionsByDate", vars); 
    resource.add(new Link(uri, "exceptionsByDate")); 
    return resource; 
} 

回答

1

你應該定義爲RequestParams名作爲參數傳遞給註解,就像這樣:

@Override 
public RepositorySearchesResource process(RepositorySearchesResource resource) { 
    TemplateVariable earlyPickupDate = new TemplateVariable(EARLY_PICKUP_DATE_PARAM, TemplateVariable.VariableType.REQUEST_PARAM, "Selects all records with earlyPickupDate >= to the value specified."); 
    TemplateVariable status = new TemplateVariable(STATUS_PARAM, TemplateVariable.VariableType.REQUEST_PARAM_CONTINUED, "Specifies the order status."); 
    TemplateVariable costCenter = new TemplateVariable(COST_CENTER_PARAM, TemplateVariable.VariableType.REQUEST_PARAM_CONTINUED, "Specified the cost center to return orders for."); 
    TemplateVariables vars = new TemplateVariables(earlyPickupDate, status, costCenter); 
    UriTemplate uri = new UriTemplate(resource.getId().getHref() + "exceptionsByDate", vars); 
    resource.add(new Link(uri, "exceptionsByDate")); 
    return resource; 
} 

private static final String EARLY_PICKUP_DATE_PARAM = "earlyPickupDate"; 
private static final String STATUS_PARAM = "status"; 
private static final String COST_CENTER_PARAM = "costCenter"; 

@RequestMapping(method = RequestMethod.GET, value = "/orders/search/exceptionsByDate") 
public @ResponseBody ResponseEntity<?> getAllExceptionsByDate(Pageable pageable, 
    @RequestParam(EARLY_PICKUP_DATE_PARAM) BigDecimal earlyPickupDate, 
    @RequestParam(STATUS_PARAM) List<String> status, 
    @RequestParam(COST_CENTER_PARAM) String costCenter) { 
    Page<OrderExceptionProjection> exceptions = orderService.getExceptions(pageable, earlyPickupDate, status, costCenter); 
    return ResponseEntity.ok(new Resources<>(exceptions)); 
} 

然後你只需定義TemplateVariables時使用相同的字符串常量