覆盖 Spring Data REST 响应处理器

有时,你可能希望为特定资源编写一个自定义处理器。为了利用 Spring Data REST 的设置、消息转换器、异常处理等功能,请使用 @RepositoryRestController 注解而不是标准的 Spring MVC @Controller@RestController。使用 @RepositoryRestController 注解的控制器会使用 RepositoryRestConfiguration.setBasePath 中定义的 API 基础路径(所有其他 RESTful 端点也使用此路径,例如 /api)提供服务。以下示例展示了如何使用 @RepositoryRestController 注解:

@RepositoryRestController
class ScannerController {

  private final ScannerRepository repository;

  ScannerController(ScannerRepository repository) { (1)
    this.repository = repository;
  }

  @GetMapping(path = "/scanners/search/producers") (2)
  ResponseEntity<?> getProducers() {

    List<String> producers = repository.listProducers(); (3)

    // do some intermediate processing, logging, etc. with the producers

    CollectionModel<String> resources = CollectionModel.of(producers); (4)

    resources.add(linkTo(methodOn(ScannerController.class).getProducers()).withSelfRel()); (5)

    // add other links as needed

    return ResponseEntity.ok(resources); (6)
  }
}
1 此示例使用构造函数注入。
2 此处理器将自定义处理方法作为查询方法资源接入。
3 此处理器使用底层 repository 获取数据,但在向客户端返回最终数据集之前会进行某种形式的后处理。
4 类型 T 的结果需要包装在 Spring HATEOAS 的 CollectionModel<T> 对象中以返回集合。EntityModel<T>RepresentationModel<T> 分别是单个项目的合适包装器。
5 添加一个链接指向该方法本身,作为 self 链接。
6 通过使用 Spring MVC 的 ResponseEntity 包装器返回集合,可以确保集合被正确包装并以适当的 Accept 类型呈现。

CollectionModel 用于表示集合,而 EntityModel(或更通用的类 RepresentationModel)用于表示单个项目。这些类型可以组合使用。如果您知道集合中每个项目的链接,可以使用 CollectionModel<EntityModel<String>>(或相应的核心领域类型,而不是 String)。这样做可以让你为每个项目以及整个集合组装链接。

在此示例中,组合路径是 RepositoryRestConfiguration.getBasePath() + /scanners/search/producers

获取聚合引用

对于接收 PUTPOST 请求的自定义控制器,请求体通常包含一个 JSON 文档,该文档将使用 URI 来表示对其他资源的引用。对于 GET 请求,这些引用通过请求参数传递。

从 Spring Data REST 4.1 开始,我们提供了 AggregateReference<T, ID>,可作为处理器方法的参数类型,用于捕获此类引用,并将其解析为引用的聚合的标识符、聚合本身或 jMolecules 的 Association。你只需声明一个该类型的 @RequestParam,然后就可以使用标识符或完全解析的聚合。

@RepositoryRestController
class ScannerController {

  private final ScannerRepository repository;

  ScannerController(ScannerRepository repository) {
    this.repository = repository;
  }

  @GetMapping(path = "/scanners")
  ResponseEntity<?> getProducers(
    @RequestParam AggregateReference<Producer, ProducerIdentifier> producer) {

    var identifier = producer.resolveRequiredId();
    // Alternatively
    var aggregate = producer.resolveRequiredAggregate();
  }

  // Alternatively

  @GetMapping(path = "/scanners")
  ResponseEntity<?> getProducers(
    @RequestParam AssociationAggregateReference<Producer, ProducerIdentifier> producer) {

    var association = producer.resolveRequiredAssociation();
  }
}

如果你正在使用 jMolecules,AssociationAggregateReference 也允许你获取一个 Association。虽然这两个抽象都假设参数的值是一个 URI,并且该 URI 匹配 Spring Data REST 用来暴露项目资源的模式,但通过在引用实例上调用 ….withIdSource(…) 可以定制源值的解析方式,以提供一个函数从接收到的 URI 中获得的 UriComponents 中提取最终用于聚合解析的标识符值。

@RepositoryRestController 对比 @BasePathAwareController

如果你对特定于实体的操作不感兴趣,但仍想在 basePath 下构建自定义操作,例如 Spring MVC 视图、资源等,请使用 @BasePathAwareController。如果你在自定义控制器上使用 @RepositoryRestController,它只会在你的请求映射与 repository 使用的 URI 空间融合时处理请求。它还会对控制器方法应用以下额外功能:

  1. 根据 handler 方法请求映射中使用的基础路径段所映射的 repository 定义的 CORS 配置。

  2. 如果使用 JPA,应用一个 OpenEntityManagerInViewInterceptor,以确保你可以访问标记为延迟解析的属性。

如果你使用 @Controller@RestController 执行任何操作,那么这段代码完全不在 Spring Data REST 的范围之内。这包括请求处理、消息转换器、异常处理以及其他用途。