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

标签 java spring spring-boot spring-data-rest

我有一个实现了 ResourceProcessor<RepositorySearchesResource> 的 Controller ,它包含以下请求映射并覆盖 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;
}

最佳答案

您应该将 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));
}

然后在定义 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;
}

关于java - 使用 Spring HATEOAS 构建模板化搜索资源 uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37191768/

相关文章:

java - Spring 启动 : how to use multiple yml files

java - Java Spring Boot bean 加载的详细日志记录?

java - 在 JSF1.1 中使用带有 DataTable 的自定义组件时出现重复 ID 错误

java - 映射器函数返回了一个空值

java - 如何在 Spring 服务的 @Transactional 方法中实现 Oracle ADF ApplicationModule 和 JPA 之间的共享事务?

java - Spring 启动 : How to override default properties in Unit tests

java - Struts Validation.xml - requiredif 问题

java - 无法在 Java 中读取 PIL 保存的 jpeg 字符串

java - 尽管设置正确,@Autowired @Service Bean 为空

git - 使用 GitHub 存储库和 SSH 登录的 Spring Boot 配置服务器