Spring boot 删除投影中的弹簧数据支撑自链接模板

Spring boot 删除投影中的弹簧数据支撑自链接模板,spring-boot,spring-data,spring-data-jpa,spring-data-rest,Spring Boot,Spring Data,Spring Data Jpa,Spring Data Rest,如你所见,我有一个嵌入式实体(患者)。它将所有数据(包括链接)返回到实体,但链接已模板化。我没有使用前端HATEOAS客户端,我也不打算改变这方面的做法。所以我需要一个普通的非模板链接。是否有任何非黑客方式可以实现此目的?您可以通过以下方式强制扩展模板: { "_embedded" : { "patient" : { "firstName" : "Kidus", "_links" : { "self" : { "href"

如你所见,我有一个嵌入式实体(患者)。它将所有数据(包括链接)返回到实体,但链接已模板化。我没有使用前端HATEOAS客户端,我也不打算改变这方面的做法。所以我需要一个普通的非模板链接。是否有任何非黑客方式可以实现此目的?

您可以通过以下方式强制扩展模板:

{
  "_embedded" : {
    "patient" : {
      "firstName" : "Kidus",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8090/api/patients/2{?projection}",
          "templated" : true
        },
    }
}
@GetMapping(“/myresources/{id}”)
公共EntityModel myResource(字符串id){
MyResource资源=。。。;
返回新的EntityModel(
资源,,
链接到(methodOn(getClass()).myResource(id)).withSelfRel().expand(id));
}

您可以使用自定义的:

@组件
公共类MyProjectionProcessor实现了RepresentationModelProcessor{
私有静态最终模式模板\u Pattern=Pattern.compile(“\\{\\?*”);
@凌驾
公共实体模型过程(最终实体模型){
//复制没有链接的模型
final EntityModel newModel=EntityModel.of(model.getContent());
//循环链接
for(最终链接:model.getLinks()){
链接newLink=链接;
//如果已模板化,则替换自链接
if(link.hasRel(IanaLinkRelations.SELF)和&link.isTemplated()){
最后一个字符串href=TEMPLATE_PATTERN.matcher(link.getHref()).replaceFirst(“”);
newLink=Link.of(href,IanaLinkRelations.SELF);
}
newModel.add(newLink);
}
返回新模型;
}
}

如果您使用Spring HATEOAS并在资源类上定义投影,则自链接将被模板化。这是Spring强制执行的约定,因此没有“非黑客”方式来改变它(但可能有一种黑客方式)。
@GetMapping("/myresources/{id}")
public EntityModel<MyResource> myResource(String id) {

    MyResource resource = ...;
    return new EntityModel<>(
                  resource,
                  linkTo(methodOn(getClass()).myResource(id)).withSelfRel().expand(id));
}