Java 使用Spring HATEOAS构建模板化搜索资源uri

Java 使用Spring HATEOAS构建模板化搜索资源uri,java,spring,spring-boot,spring-data-rest,Java,Spring,Spring Boot,Spring Data Rest,我有一个控制器,它实现ResourceProcessor,它包含以下请求映射,并覆盖进程方法,为该请求映射创建搜索uri。我这样做似乎非常脆弱,因为我将uri中的参数名指定为字符串,同时将路径指定为字符串 理想情况下,我正在寻找某种方法,可以使用在请求映射上定义的参数名构建搜索uri,这样,如果我更改了它们,就不必更改搜索uri。最后,我希望避免在uri中也将路径指定为字符串,因此我不确定是否可以根据请求映射方法名称或其他方式动态构建路径 此外,我还希望避免构建PageableTemplateV

我有一个控制器,它实现
ResourceProcessor
,它包含以下请求映射,并覆盖
进程
方法,为该请求映射创建搜索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;
}
@RequestMapping(method=RequestMethod.GET,value=“/orders/search/exceptionsByDate”)
public@ResponseBody ResponseEntity getAllExceptionsByDate(可分页、可分页、@RequestParam BigDecimal earlyPickupDate、@RequestParam列表状态、@RequestParam字符串成本中心){
页面异常=orderService.getExceptions(可分页、earlyPickupDate、状态、成本中心);
返回ResponseEntity.ok(新资源(异常));
}
@凌驾
公共RepositorySearcheSource流程(RepositorySearcheSource资源){
TemplateVariable earlyPickupDate=新的TemplateVariable(“earlyPickupeDate”,TemplateVariable.VariableType.REQUEST_参数,“选择earlyPickupDate>=为指定值的所有记录”);
TemplateVariable状态=新的TemplateVariable(“状态”,TemplateVariable.VariableType.REQUEST_PARAM_CONTINUED,“指定订单状态”);
TemplateVariable costCenter=新的TemplateVariable(“costCenter”,TemplateVariable.VariableType.REQUEST_PARAM_CONTINUED,“指定了要返回订单的成本中心”);
TemplateVariables vars=新的TemplateVariables(earlyPickupDate、状态、成本中心);
UriTemplate uri=新的UriTemplate(resource.getId().getHref()+“exceptionsByDate”,vars);
添加(新链接(uri,“exceptionsByDate”);
返回资源;
}

您应该将RequestParams的名称定义为注释的参数,如下所示:

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));
}
@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;
}