Null id property when deserialize json with jackson and Jackson2HalModule of Spring Hateoas
Actually, it was due to the Resource
class which is built to wrap the content of your bean. The content property is annotated by @JsonUnwrapped
so that the Resource
class can map your bean in this property whereas in the json, bean properties are at the same level as _links
property. With this annotation, it is possible to have property name conflict with the wrapper and the inner bean. It is exactly the case here because Resource
class has an id
property inherited from the ResourceSupport
class, and this property is sadly annotated by @JsonIgnore
.
There is a workaround for this issue. You can create a new MixIn
class inherited from the ResourceSupportMixin
class and override the getId()
method with @JsonIgnore(false)
annotation :
public abstract class IdResourceSupportMixin extends ResourceSupportMixin {
@Override
@JsonIgnore(false)
public abstract Link getId();
}
Then you just have to add your IdResourceSupportMixin
class to your ObjectMapper
:
mapper.addMixInAnnotations(ResourceSupport.class, IdResourceSupportMixin.class);
It should solve the problem.