我有一个实现ResourceProcessor<RepositorySearchesResource>
的控制器,它包含以下请求映射并覆盖process
方法以为此请求映射创建搜索uri。我这样做的方式似乎非常脆弱,因为我在uri中将参数名称指定为字符串,我还将路径指定为字符串。
理想情况下,我正在寻找一些方法,可以使用我的请求映射中定义的参数名称来构建搜索uri,这样如果我更改它们,我就不必更改搜索uri 。最后,我还想避免在uri中将路径指定为字符串,因此我不确定是否可以根据请求映射方法名称或其他方式动态构建它。
此外,我还想避免构建Pageable
TemplateVariable
。
@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;
}
答案 0 :(得分:1)
您应该将RequestParams的名称定义为注释的参数,如下所示:
struct_name2
然后在定义TemplateVariables时使用相同的String常量:
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));
}