有时,您可能希望为特定资源编写自定义处理程序。 要利用 Spring Data REST 的设置、消息转换器、异常处理等,请使用注释而不是标准的 Spring MVC 或 . 注释的控制器从 中定义的 API 基本路径提供,该路径由所有其他 RESTful 端点(例如,)使用。 以下示例演示如何使用批注:@RepositoryRestController@Controller@RestController@RepositoryRestControllerRepositoryRestConfiguration.setBasePath/api@RepositoryRestControllerSpring中文文档

@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 此处理程序使用基础存储库来获取数据,但在将最终数据集返回给客户端之前执行某种形式的后处理。
4 类型 T 的结果需要包装在 Spring HATEOAS 对象中才能返回集合。 或分别是单个项目的合适包装纸。CollectionModel<T>EntityModel<T>RepresentationModel<T>
5 将链接添加回此确切方法的链接作为链接。self
6 使用 Spring MVC 的包装器返回集合可确保集合正确包装并以正确的接受类型呈现。ResponseEntity

CollectionModel用于集合,而(或更通用的类)用于单个项目。这些类型可以组合使用。如果您知道集合中每个项目的链接,请使用(或任何核心域类型,而不是 )。这样做可以让您为每个项目以及整个集合组装链接。EntityModelRepresentationModelCollectionModel<EntityModel<String>>StringSpring中文文档

在此示例中,组合路径为 + 。RepositoryRestConfiguration.getBasePath()/scanners/search/producers
1 此示例使用构造函数注入。
2 此处理程序将自定义处理程序方法插入为查询方法资源
3 此处理程序使用基础存储库来获取数据,但在将最终数据集返回给客户端之前执行某种形式的后处理。
4 类型 T 的结果需要包装在 Spring HATEOAS 对象中才能返回集合。 或分别是单个项目的合适包装纸。CollectionModel<T>EntityModel<T>RepresentationModel<T>
5 将链接添加回此确切方法的链接作为链接。self
6 使用 Spring MVC 的包装器返回集合可确保集合正确包装并以正确的接受类型呈现。ResponseEntity
在此示例中,组合路径为 + 。RepositoryRestConfiguration.getBasePath()/scanners/search/producers

获取聚合引用

对于接收和请求的自定义控制器,请求正文通常包含一个 JSON 文档,该文档将使用 URI 来表达对其他资源的引用。 对于请求,这些引用是通过请求参数提交的。PUTPOSTGETSpring中文文档

从Spring Data REST 4.1开始,我们提供了用作处理程序方法的参数类型,以捕获此类引用并将它们解析为引用的聚合的标识符,聚合本身或jMolecules。 您需要做的就是声明该类型的 an,然后使用标识符或完全解析的聚合。AggregateReference<T, ID>Association@RequestParamSpring中文文档

@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,还可以获得 . 虽然这两个抽象都假定参数的值是与Spring Data REST用于公开项目资源的方案匹配的URI,但可以通过调用引用实例来自定义源值解析,以提供一个函数来提取标识符值,最终从从收到的URI中获取用于聚合解析。AssociationAggregateReferenceAssociation….withIdSource(…)UriComponentsSpring中文文档

@RepositoryRestController与。@BasePathAwareController

如果您对特定于实体的操作不感兴趣,但仍想在 下构建自定义操作,例如 Spring MVC 视图、资源等,请使用 . 如果在自定义控制器上使用,则仅当请求映射混合到存储库使用的 URI 空间时,它才会处理请求。 它还会将以下额外功能应用于控制器方法:basePath@BasePathAwareController@RepositoryRestControllerSpring中文文档

  1. CORS 配置根据为映射到处理程序方法的请求映射中使用的基路径段的存储库定义。Spring中文文档

  2. 应用 if JPA 以确保您可以访问标记为要延迟解析的属性。OpenEntityManagerInViewInterceptorSpring中文文档

如果您使用 or 用于任何内容,则该代码完全超出了 Spring Data REST 的范围。这扩展到请求处理、消息转换器、异常处理和其他用途。@Controller@RestController
如果您使用 or 用于任何内容,则该代码完全超出了 Spring Data REST 的范围。这扩展到请求处理、消息转换器、异常处理和其他用途。@Controller@RestController