I tried to reuse HalLinkListSerializer in one of my ResourceSupport objects to render a list of links using
@JsonSerialize(using = Jackson2HalModule.HalLinkListSerializer.class)
It worked under one scenario, but on another Jackson would throw an exception saying that Jackson2HalModule$HalLinkListSerializer has no default constructor.
I have a temporary workaround using this
public static class CustomHalLinkListSerializer extends Jackson2HalModule.HalLinkListSerializer {
public CustomHalLinkListSerializer() {
super(null, null);
}
}
I don't quite get why you need this in the first place. ResourceSupport already has a list of links. In fact, carrying that list is the sole reason for existence of the class. If you need to manually use the HalLinkListSerializer (although I doubt that's necessary) register the HalHandlerInstantiator with your Jackson setup. This will cause Jackson to create instances of the serializer appropriately.
@olivergierke thanks for the quick reply.
I am using ResourceSupport and I would also like to render another list of links in the same object called webLinks. I know these aren't really HATEOAS links to follow, but I wanted to reuse the Link object anyway.
@JsonSerialize(using = CustomHalLinkListSerializer.class)
public List<Link> getWebLinks() {
Where can I get an example on how to use HalHandlerInstantiator? I don't see it on the README. FYI, the API link for 0.12.0 on http://projects.spring.io/spring-hateoas/ isn't working.
We are using Spring Boot BTW and I have @EnableEntityLinks and @EnableHypermediaSupport(type=HypermediaType.HAL) enabled.
This is probably an issue even now!
This works as designed. HalHandlerInstantiator will take care of creating instances of HalLinkListSerializer, i.e. you need to register that with the ObjectMapper that serializes your objects. Spring HATEOAS equipped HttpMessageConverters responsible for rendering HAL already have that configured.
I am getting this exception:
[org.springframework.http.converter.json.MappingJackson2HttpMessageConverter] Failed to evaluate Jackson serialization for type [class de.zalando.godfathers.employee.management.model.EmployeeResponse]:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.hateoas.mediatype.hal.Jackson2HalModule$HalLinkListSerializer':
Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.hateoas.mediatype.hal.Jackson2HalModule$HalLinkListSerializer]:
No default constructor found; nested exception is java.lang.NoSuchMethodException:
org.springframework.hateoas.mediatype.hal.Jackson2HalModule$HalLinkListSerializer.<init>()
my model is:
data class EmployeeResponse(
val employees: List<EmployeeInformation>
) : EntityModel<EmployeeResponse>()
data class EmployeeInformation(
val employeeId: String,
val email: String? = null,
val name: Name? = null,
val discountAccounts: CollectionModel<DiscountAccount>? = null
) : EntityModel<EmployeeInformation>()
data class Name(
val firstName: String,
val lastName: String
)
data class DiscountAccount(
val id: String,
val active: Boolean,
val countryCode: String,
val coupons: CollectionModel<Coupon>? = null
) : EntityModel<DiscountAccount>()
data class Coupon(
val id: String,
val code: String,
val active: Boolean
)
HalHandlerInstantiator doesn't know anything about your custom extension of HalLinkListSerializer and I don't see too much value in opening all of this up for extension in the first place as we need to tweak the internals on a regular basis and it would ver quickly become an API maintenance nightmare.
The original poster's problem and the workaround should not be needed if all you want to do is reuse the existing implementations.
then I think I am facing a different problem that has the same symptom. I am using HATEOAS 1.0.2.RELEASE. Do you think I should open a new issue for this? Also, I am not using anything custom here
Most helpful comment
then I think I am facing a different problem that has the same symptom. I am using HATEOAS 1.0.2.RELEASE. Do you think I should open a new issue for this? Also, I am not using anything custom here