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

时间:2019-06-16 19:12:10

标签: spring spring-data-rest spring-hateoas jackson2

如何配置 Spring Data REST 以删除存储库接口端点的集合资源响应上的实体关联链接(仅保留为“自身”),而无需设置 exported = @ResResource注释上的错误(需要继续导出端点)

我们有一些实体,其中 _links 部分的响应大小最大:

    Item resource上的
  • _ 链接可以用于浏览关联。

  • 但是在Collection Resources上(主要是在大型馆藏上),此信息并不重要,并使响应不必要地变大。

我们需要更改此响应:

    {
     "_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]
    }

对_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 个答案:

答案 0 :(得分:0)

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

{'gs_designation': 'A506',
 'gs_homebook': '0103 CS01',
 'gs_year': '16',
 'mc_addtocart': 'PDF-A506',
 'mc_date': '2016',
 'mc_dltype': 'allstd,active,basecompass',
 'mc_doctype': 'Active Standard',
 'mc_doi': 'A0506-16',
 'mc_icsdata': '77.140.50 (Flat steel products and semi-products)',
 'mc_keywordsen': 'cold-rolled steel products~ hot-rolled steel products~ ',
 'mc_language': 'English',
 'mc_login': 'true',
 'mc_maincat': 'standard,sedl',
 'mc_maincomm': 'A01',
 'mc_relatedurl': 'A506_related.htm',
 'mc_section': '01',
 'mc_sectors': 'Metals',
 'mc_sublevel': 'standards-and-publications,sedl-digital-library',
 'mc_suburl': '/SUBSCRIPTION/filtrexx40.cgi?+/usr6/htdocs/newpilot.com/SUBSCRIPTION/REDLINE_PAGES/A506.htm',
 'mc_tax0': 'Properties_and_Measurements,Test_Methods,Materials',
 'mc_tax1': 'Chemical_Properties,Mechanical_Test,Metals_--_Iron_and_Alloys',
 'mc_tax2': 'Chemical_Composition,Fractography,Tensile_Test,Steel,Iron_and_Steel_Products',
 'mc_tax3': 'Hardness_Test,Alloy_Steel,Flat_Products,Specialty_Steel',
 'mc_tax4': 'Structural_Steel',
 'mc_taxkeywordsen': 'Alloy Steel,Flat Products,Structural Steel,Chemical '
                     'Composition,Hardness Test,Tensile Test',
 'mc_tertiary': 'standards-products',
 'mc_toplevel': 'products-and-services',
 'mc_unspscdata': '30264100(Steel alloy sheets)',
 'title': 'Standard Specification for Alloy and Structural Alloy Steel, Sheet '
          'and Strip, Hot-Rolled and Cold-Rolled'}
********************************************************************************
{'gs_designation': 'B768',
 'gs_homebook': '0201 CS02',
 'gs_year': '11(2016)',
 'mc_addtocart': 'PDF-B768',
 'mc_date': '2016',
 'mc_dltype': 'allstd,active,basecompass',
 'mc_doctype': 'Active Standard',
 'mc_doi': 'B0768-11R16',

...and so on.

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

@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) );
}

并且ResourceAssembler使用资源

@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;
    }

}

所有这些都在RestPracticeWithHateos