Open asarkar opened 8 years ago
I encountered this multiple links issue today. It was a slightly different scenario but I managed to fix it by letting my resource extend ResourceSupport.
It's recommended for any Spring MVC/WebFlux controller to return one of HATEOAS's "types", ie a subclass of RepresentationModel
in order for it to properly render hypermedia.
I just faced the same problem with 3.2.0
.
The following base class generates two $.links
property.
@JsonInclude(JsonInclude.Include.NON_NULL)
@Setter
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
//@SuperBuilder
@SuppressWarnings({
"java:S101", // class __Base...
"java:S119" // <SELF ...>
})
public abstract class __BaseType<SELF extends __BaseType<SELF>>
extends RepresentationModel<SELF>
implements Serializable {
/**
* Returns all {@link Link}s contained in this resource.
*
* @return all {@link Link}s contained in this resource.
* @apiNote Overridden to be annotated with {@link Schema.AccessMode#READ_ONLY} and
* {@link JsonProperty.Access#READ_ONLY}.
*/
@Schema(accessMode = Schema.AccessMode.READ_ONLY)
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
@Override
public Links getLinks() {
return super.getLinks();
}
}
{
"links" : [ ],
...
"links" : {
"self" : {
"href" : "http://localhost:56923/api/stores/..."
},
"store" : {
"href" : "http://localhost:56923/api/stores/..."
}
}
}
Vanilla resource
User
-Search result with hostname changed in the URL - 2
_links
presentThe duplicate links are not present when the resource is accessed on it's own. However, when a client (a microservice in my case) makes a request using a
RestTemplate
, the response comes back with 2_links
as shown above.I'm using Spring Data JPA, Spring Data Rest, Spring HATEOAS and Spring Cloud. Here's a link to the project on my Github.