当前位置: 首页 > 知识库问答 >
问题:

为Spring Data REST删除集合资源上内容的关联链接

章烨烨
2023-03-14

我们需要改变这种反应:

    {
     "_embedded" : {
     "persons" : [ {
       "id" : "bat_3191",
       "name" : "B",
       "_links" : {     // 80% of response size !!
            "self" : {
                "href" : "http://localhost:8080/api/persons/bat_3191"
            },
            "orders" : {
                "href" : "http://localhost:8080/api/persons/bat_3191/order"
            },
            "payments" : {
                "href" : "http://localhost:8080/api/persons/bat_3191/payments"
            },
            "childrens" : {
                "href" : "http://localhost:8080/api/persons/bat_3191/childrens"
            },
            "invoices" : {
                "href" : "http://localhost:8080/api/persons/bat_3191/invoices"
            },
            "brands" : {
                "href" : "http://localhost:8080/api/persons/bat_3191/brands"
            },
        }
      },
      { person [2] } 
        ... 
      { person [N] }
      ]
    },
      "_links" : {
       [page links]
    }

到_链接上的唯一“自我”部分:

{
"_embedded" : {
"persons" : [ {
  "id" : "bat_3191",
  "name" : "B",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/api/persons/bat_3191"
    }
  }
}, {
  "id" : "bat_2340",
  "name" : "B",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/api/persons/bat_2340"
    }
  }

共有1个答案

易星宇
2023-03-14

如果我想控制springboot中的hateos链接,我使用了一个资源汇编程序。例如:

@Autowired
private EmployeeAddressResourceAssembler assembler;

@GetMapping(value="/{empId}", produces=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<EmployeeAddressResource> getEmployeeAddress(@PathVariable Integer empId) {
    EmployeeAddressItem employeeAddressItem = restTemplate.getForObject( 
        serviceUrl + "/employee/address/{empId}", 
        EmployeeAddressItem.class, empId);
    return ResponseEntity.ok( assembler.toResource(employeeAddressItem) );
}

然后我使用了资源汇编器。

@Component
public class EmployeeAddressResourceAssembler
        extends ResourceAssemblerSupport<EmployeeAddressItem, EmployeeAddressResource> {

    public EmployeeAddressResourceAssembler() {
        super(EmployeeAddressController.class, EmployeeAddressResource.class);
    }

    @Override
    public EmployeeAddressResource toResource(EmployeeAddressItem item) {

        // createResource(employeeAddressItem);
        EmployeeAddressResource resource = createResourceWithId(item.getEmpId(), item);
        resource.fromEmployeeAddressItem(item);
        // … do further mapping
        resource.add(linkTo(methodOn(EmployeeAddressController.class).deleteEmployeeAddress(item.getEmpId())).withRel("delete"));        
        return resource;
    }

}

ResourceAssembler使用资源

public class EmployeeAddressResource extends ResourceSupport {
    private Integer empId;
    private String address1;
    private String address2;
    private String address3;
    private String address4;
    private String state;
    private String country;

    public void fromEmployeeAddressItem(EmployeeAddressItem item) {
        this.empId = item.getEmpId();
        this.address1 = item.getAddress1();
        this.address2 = item.getAddress2();
        this.address3 = item.getAddress3();
        this.address4 = item.getAddress4();
        this.state = item.getState();
        this.country = item.getCountry();
    }
 类似资料:
  • 问题内容: 我的iPhone应用程序中有一个webview,并且我的Resources文件夹中也有一些html文件。加载应用程序后,我会将资源中的页面加载到Web视图中。但是,我需要在Web视图中建立指向其他资源的链接(例如,图像或其他html文件)。只做一个相对链接是行不通的: 问题答案: 加载这些资源时,需要设置基本URL。您可以使用以下方法执行此操作: …其中baseURL是资源文件夹的文件

  • 在 MongoDB 中,可以使用 drop() 方法来从数据库中删除指定集合,它会从数据库中完全删除一个集合,并且不会留下与已删除集合关联的任何索引。 drop() 方法在使用时不需要带有任何参数,并且在使用参数调用时会产生错误,该方法的语法格式如下: db.collection_name.drop() 其中 collection_name 为要删除的集合名称,方法调用成功会返回 true,否则返

  • 接口说明 如果已建立了主屏与副屏之间的关联,而由于某些原因,不想再进行两个场景之间数据的差异对比,Wish3DEarth团队新增了删除关联场景的接口,开发者可以通过该接口,来解除主屏与副屏之间的关联性。 如需调用,请访问 开发者文档 来查看详细的接口使用说明 该接口仅开放给已获取SDK的开发者 API地址 POST /api/contrasts/1.0.0/del 是否需要登录 是 请求字段说明

  • 了解如何跨云文档使用链接资源,以提高各工作流程的工作效率。 随着设计界面的不断丰富,设计人员需要轻松协作来创建和维护一致的设计。借助链接资源,设计人员可以在云文档中创建一个基础的上下文库,包含一致扩展其设计所需的所有资源(颜色、字符样式和组件)。 借助最新版本的 Adobe XD,您不仅可以轻松共享和使用组件(以前称为符号),还可以轻松共享和使用颜色及字符样式,只需几个简单的步骤。如果您要与团队分

  • 在本章中,让我们研究一下Deleting content 。 您可以删除任何以前编写的内容,例如Articles , Basic page和Blog entry 。 以下是用于删除内容的简单步骤。 Step 1 - 单击“ Content 。 Step 2 - 您可以删除About us (关于我们页面是在Drupal - 创建内容一章中创建的) ,然后单击delete选项卡,如以下屏幕所示。 S