Skip to content

Jackson2HalModule.HalLinkListSerializer needs default constructor #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
csavory opened this issue Jun 10, 2014 · 9 comments
Closed

Jackson2HalModule.HalLinkListSerializer needs default constructor #196

csavory opened this issue Jun 10, 2014 · 9 comments
Assignees
Labels
in: core Core parts of the project in: mediatypes Media type related functionality resolution: works as designed

Comments

@csavory
Copy link

csavory commented Jun 10, 2014

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);
        }

    }
@odrotbohm
Copy link
Member

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.

@odrotbohm odrotbohm self-assigned this Jun 10, 2014
@csavory csavory changed the title Jackson2HalModule.HalLinkListSerializer need default constructor Jackson2HalModule.HalLinkListSerializer needs default constructor Jun 10, 2014
@csavory
Copy link
Author

csavory commented Jun 10, 2014

@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.

@piyushmor
Copy link

This is probably an issue even now!

@odrotbohm odrotbohm added in: core Core parts of the project in: mediatypes Media type related functionality resolution: works as designed labels Dec 11, 2019
@odrotbohm
Copy link
Member

odrotbohm commented Dec 11, 2019

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.

@piyushmor
Copy link

piyushmor commented Dec 11, 2019

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>()

@piyushmor
Copy link

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
)

@odrotbohm
Copy link
Member

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.

@piyushmor
Copy link

piyushmor commented Dec 11, 2019

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

@MikeTraceur
Copy link

MikeTraceur commented May 24, 2023

maybe it will help others too. I faced the same Warning and couldn't find the solution. after debugging a bit in spring dependencies I realized my issue. I missed the produces part in my Get/Put/PostMapping:

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

@RestController
@RequestMapping("/")
public class SubmitController {

	@Autowired
	private SubmitService submitService;

	@PostMapping(value = "/submit", produces = MediaTypes.HAL_JSON_VALUE) // don't leave produces empty or default
	public EntityModel<T> submit(@Valid @RequestBody SubmitEntity submitEntity,
		SubmitAnswer submitAnswer = submitService.submit(submitEntity);
		return EntityModel.of(submitAnswer, linkTo(methodOn(getClass()).submit()).withSelfRel());
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Core parts of the project in: mediatypes Media type related functionality resolution: works as designed
Projects
None yet
Development

No branches or pull requests

4 participants