Projection REST 响应中的重复链接键

Duplicate _links key in Projection REST responcse

为了在投影的 Json 框架中添加我的链接,我添加了以下内容 class:

@Component
public class ResumeEntityProjectionResourceProcessor implements ResourceProcessor<Resource<ResumeEntity>> {

@Override
public Resource<ResumeEntity> process(Resource<ResumeEntity> resource) {

    UriComponents uriComponents = ServletUriComponentsBuilder.fromCurrentContextPath()
            .path("/api/entities/search/findEntities")
            .buildAndExpand(Long.toString(resource.getContent().getId()));
    resource.add(new Link(uriComponents.toUriString(), "findEntities"));

    return resource;
}

作为 return 值,我现在遇到的问题是两个键“_links”,一个是默认链接加上我的链接,另一个是我的链接。

{
"_embedded": {
    "entities": [{
            "id": 1696,
            "reference": "aaaaaa",
            "_links": {
                "self": {
                    "href": "http://localhost:8080/myProject/api/entities/1696{?projection}",
                    "templated": true
                },
                "findByParametresValide": {
                    "href": "http://localhost:8080/myProject/api/entities/search/findEntities"
                }
            },
            "_links": {
                "findByParametresValide": {
                    "href": "http://localhost:8080/myProject/api/entities/search/findEntities"
                }
            }
        }
    ]
}
}

我是怎么做到只保留第一个标签的?

预期结果

{
"_embedded": {
    "entities": [{
            "id": 1696,
            "reference": "aaaaaa",
            "_links": {
                "self": {
                    "href": "http://localhost:8080/myProject/api/entities/1696{?projection}",
                    "templated": true
                },
                "findByParametresValide": {
                    "href": "http://localhost:8080/myProject/api/entities/search/findEntities"
                }
            }
        }
    ]
}
}

谢谢

@Override
public Resource<ResumeEntity> process(Resource<ResumeEntity> resource) {
if (resource.getLinks().isEmpty()) { 
return resource; 
}
    UriComponents uriComponents = ServletUriComponentsBuilder.fromCurrentContextPath()
            .path("/api/entities/search/findEntities")
            .buildAndExpand(Long.toString(resource.getContent().getId()));
    resource.add(new Link(uriComponents.toUriString(), "findEntities"));

    return resource;
}