spring-guides / tut-rest

Building REST services with Spring :: Learn how to easily build RESTful services with Spring

Home Page:https://spring.io/guides/tutorials/rest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting `javax.xml.bind.MarshalException` in "What makes something RESTful?" part of tutorial

mlvandijk opened this issue · comments

I've replaced the @GetMapping("/employees/{id}") and @GetMapping("/employees") methods as specified under the "What makes something RESTful?" heading.

My application was working before I made these changes.

When I go to http://localhost:8080/employees, I get the following:

Whitelabel Error PageThis application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Oct 19 18:23:16 CEST 2018There was an unexpected error (type=Internal Server Error, status=500).
Could not marshal [Resources { content: [Resource { content: Employee(id=1, name=Bilbo Baggins, role=burglar), links: [<http://localhost:8080/employees/1>;rel="self", <http://localhost:8080/employees>;rel="employees"] }, 
Resource { content: Employee(id=2, name=Frodo Baggins, role=thief), links: [<http://localhost:8080/employees/2>;rel="self", <http://localhost:8080/employees>;rel="employees"] }], links: [<http://localhost:8080/employees>;rel="self"] }]: null; 
nested exception is javax.xml.bind.MarshalException - with linked exception: [com.sun.istack.internal.SAXException2: unable to marshal type "org.springframework.hateoas.Resource" as an element because it is not known to this context.]

When I go to http://localhost:8080/employees/1, I get the following:

Whitelabel Error PageThis application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Oct 19 18:33:45 CEST 2018There was an unexpected error (type=Internal Server Error, status=500).
Could not marshal [Resource { content: Employee(id=1, name=Bilbo Baggins, role=burglar), links: [<http://localhost:8080/employees/1>;rel="self", <http://localhost:8080/employees>;rel="employees"] }]: null; 
nested exception is javax.xml.bind.MarshalException - with linked exception: [com.sun.istack.internal.SAXException2: class nl.marit.springdemo.payroll.Employee nor any of its super class is known to this context. javax.xml.bind.JAXBException: class xxxxx.payroll.Employee nor any of its super class is known to this context.]

Sidenote: My IDE (IntelliJ) also wants to add imports for linkTo and methodOn, while I cannot find imports for these in rest/src/main/java/payroll/EmployeeController.java.

Link to my project.

As for the imports -

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;

Calling the url using a browser (ie Google Chrome) causes this issue - it seems the browser prefers an XML response and Spring adheres to that.. Using something like Postman avoids the issue and returns the expected result.

Good to know I had the right imports. I'd like to find a way to do the tutorial without having to use additional tools like Postman, but good to know where the error comes from. Thank you!

Indeed. Spring HATEOAS had XML support before we added HAL support. Long story short, the JAXB annotations are still there, but not very usable.

See spring-projects/spring-hateoas#695.

You can access the data through a browser as well as CURL if you change all GetMapping annotations to something like this:

@GetMapping(value = "/employees", produces = "application/json; charset=UTF-8")

As of spring-projects/spring-hateoas@dca65e1, the JAXB annotations have been removed and should no longer cause this issue

However you may have to wait for Spring Data REST to catch up to Spring HATEOAS 1.0.0.BUILD-SNAPSHOT.

I have the same "jaxbexception", and thank @Lance-Drane ,who solves it.