Reputation: 105
I am trying to add HATEOAS links to my Controller results. Link creation:
Link link = JaxRsLinkBuilder.linkTo(MainRestService.class)
.slash(this::getVersion)
.slash(LinkMappingConstants.TAXPAYERS)
.slash(taxPayerId)
.slash(LinkMappingConstants.ESTIMATIONS)
.slash(estimationId)
.withSelfRel();
The link itself is created fine, but there are superfluous entries in the resulting JSON:
"links": [
{
"rel": "self",
"href": "http://localhost:8080/blablabla",
"hreflang": null,
"media": null,
"title": null,
"type": null,
"deprecation": null,
"template": {
"variables": [
],
"variableNames": [
]
}
}
]
How can i get this format? (without use property spring.jackson.default-property-inclusion=NON_NULL
)
"links": [
{
"rel": "self",
"href": "http://localhost:8080/blablabla"
}
]
Thanks.
Upvotes: 1
Views: 484
Reputation: 1015
As you mention, if you want NON_NULL
property inclusion on the whole JSON and not just links
you can use spring.jackson.default-property-inclusion=NON_NULL
. This won't fix the empty template fields however.
If you want to have NON_NULL
property inclusion on just the Link
object using Jackson for serialization you can achieve this by using a Jackson MixIn for the Link
object with the @JsonInclude(Include.NON_NULL)
annotation.
As an example:
@JsonInclude(Include.NON_NULL)
abstract class LinkMixIn {
}
mapper.addMixIn(Link.class, LinkMixIn.class);
To hide the template fields you can either add @JsonIgnore if you never want the template section to be serialized, or try NON_DEFAULT
property inclusion on the above answer which creates a new instance of the object and compares it to what is to be serialized to determine if it should be included.
E.g. something like the following would not serialize the result of getTemplate at all
@JsonInclude(Include.NON_NULL)
abstract class LinkMixIn {
@JsonIgnore abstract Template getTemplate();
}
Upvotes: 1