1
votes

I'm trying to run the hateoas spring boot example located at https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-hateoas (I'm trying to run Version 1.2.6.RELEASE).

When accessing the customers service provided by the example I get a MarshalException:

Could not marshal [Resources { content: [sample.domain.Customer@72c01d5a, sample.domain.Customer@5ef1ebf9, sample.domain.Customer@237de11a], links: [http://localhost:8080/customers;rel="self"] }]: null; nested exception is javax.xml.bind.MarshalException - with linked exception: [com.sun.istack.internal.SAXException2: class sample.domain.Customer nor any of its super class is known to this context. javax.xml.bind.JAXBException: class sample.domain.Customer nor any of its super class is known to this context.]

This is caused by the following code:

    @RequestMapping(method = RequestMethod.GET)
HttpEntity<Resources<Customer>> showCustomers() {
    Resources<Customer> resources = new Resources<Customer>(this.repository.findAll());
    resources.add(this.entityLinks.linkToCollectionResource(Customer.class));
    return new ResponseEntity<Resources<Customer>>(resources, HttpStatus.OK);
}

While I could probably implement my own

public class CustomerResources extends Resources<Customer> {

I'd like to avoid this if at all possible.

Also if it has to be like this I'm confused why it's not like this in the example. Am I missing something? How can I get the sample to work?

Thanks in advance!

2
Do really want to send XML? - a better oliver
I would be find with not sending XML but I'd really like the URLs to work by default from a browser - Patrick Huy

2 Answers

2
votes

The sample is intended to work with JSON and will serve JSON by default. I guess you're sending a request with an Accept header that requests application/xml. Try removing the header, or requesting application/json instead.

0
votes

While working with HATEOAS, Exception: javax.xml.bind.MarshalException occurs when you do not add correct producer type with URI.

Whenever you use HATEOAS in Spring boot, you need to add produces type as application/hal+json.

Example: @PostMapping(value="/yourUri",produces = "application/hal+json")

This worked for me!