Hi I am using spring data rest and Ihave a weird issue when I am using PagingAndSortingRepository the response I get has a _self link as expected but with it also gives a duplicate link of the same entity hf:foo as evindent in the below response.
dupicate links "self" : { "href" : "http://localhost:8080/foos/8445" } and "hf:foo" : { "href" : "http://localhost:8080/foos/8445"
curl http://localhost:8080/foos?page=0&size=1
{
"_links" : {
"first" : {
"href" : "http://localhost:8080/foos?page=0&size=1"
},
"prev" : {
"href" : "http://localhost:8080/foos?page=0&size=1"
},
"self" : {
"href" : "http://localhost:8080/foos"
},
"next" : {
"href" : "http://localhost:8080/foos?page=2&size=1"
},
"last" : {
"href" : "http://localhost:8080/foos?page=81&size=1"
}
},
"_embedded" : {
"hf:foos" : [ {
"name" : "comsi",
"_links" : {
"self" : {
"href" : "http://localhost:8080/foos/8445"
},
"hf:foo" : {
"href" : "http://localhost:8080/foos/8445"
} ]
}
} ]
},
"page" : {
"size" : 1,
"totalElements" : 82,
"totalPages" : 82,
"number" : 1
}
}
please help .Is it an issue with the curie namespace .
What you're seeing here is not a duplicate link as you're claiming. One is the self
link that needs to be canonical by definition (i.e. no template parameters).
The other, additional link exposes the resource type (in this case the item resource of a foo
. This allows clients to reason about what they expect (what representation, which HTTP verbs to use) when following the link.
Also, that particular link will expose template variables that might apply to that resource. E.g. if a projection is available for Foo
instances, the links for the embedded document will look something like this.
{
_links : {
self : { href : "…/foos/8445"},
ht:foo : { href : "…/foos/8445{?projection}" }
}
}
Now you could argue that the additional link doesn't need to be there if it's just the same as the self
link. That would the require the client to be more complex as it basically needs to guard access with a "if there, then use, if not, do something different". That's why we decided to always render it, just to make sure clients that look for resource type based links find them no matter what.
If you want to remove that "duplicated" link you can add this Bean:
@Bean
public ResourceProcessor<Resource<?>> entityProcessor() {
return new ResourceProcessor<Resource<?>>() {
@Override
public Resource<?> process(Resource<?> resource) {
resource.removeLinks();
return resource;
}
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With