简体   繁体   English

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

[英]Building a templated search resource uri with Spring HATEOAS

I have a controller which implements ResourceProcessor<RepositorySearchesResource> , it contains the following request mapping and overrides the process method to create a search uri for this request mapping.我有一个实现ResourceProcessor<RepositorySearchesResource>的控制器,它包含以下请求映射并覆盖process方法以为此请求映射创建搜索 uri。 The way I've done it seems very brittle as I'm specifying parameter names in the uri as strings, I'm also specifying the path as a string.我这样做的方式似乎很脆弱,因为我将 uri 中的参数名称指定为字符串,我还将路径指定为字符串。

Ideally, I'm looking for some way that I could build the search uri using the parameter names defined on my request mapping, that way if I change them, I don't have to change the search uri.理想情况下,我正在寻找某种方式,我可以使用在我的请求映射中定义的参数名称来构建搜索 uri,这样如果我更改它们,我就不必更改搜索 uri。 Lastly, I'd like to avoid specifying the path as a string as well in the uri, so I'm not sure if that can be built dynamically based on the request mapping method name or some other means.最后,我想避免在 uri 中也将路径指定为字符串,所以我不确定是否可以根据请求映射方法名称或其他方式动态构建。

Also, I would like to avoid building the Pageable TemplateVariable as well.另外,我也想避免构建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;
}

You should define the names for the RequestParams as arguments to the annotation, like this:您应该将 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));
}

Then you just use the same String constants when defining the TemplateVariables:然后在定义 TemplateVariables 时使用相同的字符串常量:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM