0

to add my links in the Json frame of a projection I added the following 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;
}

The problem I had now as a return value was two keys "_links" one with the default links plus my links and the other with my links only.

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

How I did to keep only the first tag?

the expected result

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

thanks

Dali
  • 135
  • 2
  • 13
  • Try adding your link to resource.[getLinks()](https://docs.spring.io/spring-hateoas/docs/current/api/org/springframework/hateoas/ResourceSupport.html#getLinks--). – Marc Tarin Jun 13 '18 at 12:55
  • To solve the problem I added the test on the resource object below `@Override public Resource process(Resource 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; }` – Dali Jun 13 '18 at 13:57

1 Answers1

0
@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;
}
Dali
  • 135
  • 2
  • 13