I have a JAX-RS webservice that makes use of JPA entity classes. I have a resource class like this:
@Path("/entity")
public class MyEntityResource
{
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/{entity}")
public MyEntity getMyEntity(@PathParam("entity") String entity)
{
log.debug("Entering getMyEntity with param: " + entity);
MyEntity entityObject = genericService.find(MyEntity.class, entity);
if (entityObject == null)
{
log.debug("Entity not found.");
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
log.debug("Exiting getMyEntity");
return entityObject;
}
}
When I run the service and do a get call on the entity, I get this error:
SEVERE: The response of the WebApplicationException cannot be utilized as the response is already committed. Re-throwing to the HTTP container
javax.ws.rs.WebApplicationException: javax.xml.bind.MarshalException
- with linked exception:
[Exception [EclipseLink-25003] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred marshalling the object
Internal Exception: java.lang.NullPointerException]
at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:151)
...
<snip>
...
Caused by: javax.xml.bind.MarshalException
- with linked exception:
[Exception [EclipseLink-25003] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred marshalling the object
Internal Exception: java.lang.NullPointerException]
at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:271)
at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:171)
at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:149)
... 32 more
Caused by: Exception [EclipseLink-25003] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred marshalling the object
Internal Exception: java.lang.NullPointerException
at org.eclipse.persistence.exceptions.XMLMarshalException.marshalException(XMLMarshalException.java:76)
at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:502)
at org.eclipse.persistence.jaxb.JAXBMarshaller.marshal(JAXBMarshaller.java:269)
... 34 more
Caused by: java.lang.NullPointerException
at org.eclipse.persistence.exceptions.ConversionException.couldNotBeConverted(ConversionException.java:69)
...
<snip>
...
at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:916)
at org.eclipse.persistence.oxm.XMLMarshaller.marshal(XMLMarshaller.java:468)
... 35 more
Nowhere in any of the stack traces are any of my classes referenced. Also, the "Exiting getMyEntity"
statement is logged before the exception.
I have no idea what is throwing the NPE or how to debug this.
Before this error, I was getting a [com.sun.istack.SAXException2: A cycle is detected in the object graph. This will cause infinitely deep XML:
from my JPA (EclipseLink) entity classes and I added the annotation MOXy @XmlInverseReference to my child class on the parent getter method.
Any thoughts on what could be throwing this exception?
Did you include a jaxb.properties file in with your entity classes?
Check out Blaise's answer to this question: JAXB Mapping cyclic references to XML
Hope this helps. RG
We have had some offline discussion about this issue, and for the benefit of others that find this post below is the correct setup of using @XmlInverseReference at multiple levels:
Entity A
Entity B
Entity C
Demo
Demo - Output
input.xml
This issue is also being handled on the EclipseLink forums, for more information see:
Below is another example of using @XmlInverseReference with a JPA model