We are using Jersey 1.10.0 and are experiencing the following issue
while unmarshalling our JAXB objects.
We have the following method to accept POST requests.
@POST
@Path("/captureVotes")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response captureVotes(VoteListJAXB votes, @Context HttpHeaders
httpHeaders) throws ValidationException, Exception {
do some stuff
}
Consider the following POST which ends in a HTTP 500 error from the
server.
POST /eventing/vote/rest/captureVotes HTTP/1.1
User-Agent: Jakarta Commons-HttpClient/3.1
Host: anywhere.com
Content-Length: 292
Content-Type: application/xml; charset=UTF-8
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><votes><vote
email="john.doe_at_anywhere.com" facebookid="43465362" firstname="john"
lastname="doe" phone="2125551234"/><vote
email="john2.doe2_at_anywhere.com" facebookid="73465362" firstname="john"
lastname="doe" phone="2125551235"/></votes>
It is failing during the unmarshall step prior to invoking the
captureVotes method with the following stacktrace.
java.lang.NullPointerException
at
com.sun.xml.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(L
ister.java:305)
at
com.sun.xml.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(L
ister.java:269)
at
com.sun.xml.bind.v2.runtime.unmarshaller.Scope.add(Scope.java:121)
at
com.sun.xml.bind.v2.runtime.property.ArrayERProperty$ReceiverImpl.recei
ve(ArrayERProperty.java:213)
at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElemen
t(UnmarshallingContext.java:523)
at
com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.endElement(SAXCon
nector.java:160)
at
org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
at
org.apache.xerces.parsers.AbstractXMLDocumentParser.emptyElement(Unknow
n Source)
at
org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknow
n Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDi
spatcher.dispatch(Unknown Source)
at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unkn
own Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown
Source)
at
org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown
Source)
at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Un
marshallerImpl.java:216)
at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unm
arshallerImpl.java:189)
at
javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmar
shallerImpl.java:120)
at
javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmar
shallerImpl.java:103)
at
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider.readFro
m(XMLRootElementProvider.java:115)
at
com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(
AbstractRootElementProvider.java:111)
at
com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerReques
t.java:474)
at
com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchPro
vider$EntityInjectable.getValue(EntityParamDispatchProvider.java:123)
at
com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectabl
eValues(InjectableValuesProvider.java:46)
at
com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethod
DispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodD
ispatchProvider.java:153)
at
com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethod
DispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDis
patchProvider.java:203)
at
com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDisp
atcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at
com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRu
le.java:288)
at
com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHand
PathRule.java:147)
at
com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceC
lassRule.java:108)
at
com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHand
PathRule.java:147)
at
com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(Roo
tResourceClassesRule.java:84)
at
com.sun.jersey.server.impl.application.WebApplicationImpl._handleReques
t(WebApplicationImpl.java:1469)
at
com.sun.jersey.server.impl.application.WebApplicationImpl._handleReques
t(WebApplicationImpl.java:1400)
at
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest
(WebApplicationImpl.java:1349)
at
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest
(WebApplicationImpl.java:1339)
at
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.
java:416)
at
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletCo
ntainer.java:537)
at
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletCo
ntainer.java:708)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Applic
ationFilterChain.java:290)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFil
terChain.java:206)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperVal
ve.java:233)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextVal
ve.java:191)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.jav
a:127)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.jav
a:102)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve
.java:109)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:
298)
at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:8
52)
at
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process
(Http11Protocol.java:588)
at
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.
java:396)
at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecut
or.java:886)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.j
ava:908)
at java.lang.Thread.run(Thread.java:619)
We setup the folowing test GET method with the same VoteListJAXB to
inspect the returned XML format.
@GET
@Path("/getVote")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public VoteListJAXB getVote() {
manually populate a couple VoteJAXB objects to return
}
It correctly returns the following XML which we have been using in the
above POST test.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><votes><vote
email="john.doe_at_anywhere.com" facebookid="43465362" firstname="john"
lastname="doe" phone="2125551234"/><vote
email="john2.doe2_at_anywhere.com" facebookid="73465362" firstname="john"
lastname="doe" phone="2125551235"/></votes>
Here are the source for VoteListJAXB and VoteJAXB objects.
import java.io.Serializable;
import java.util.Set;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "votes")
public class VoteListJAXB implements Serializable {
private static final long serialVersionUID =
1318919138435182502L;
private Set<VoteJAXB> voteJAXBSet = null;
/**
* @return the voteJAXBSet
*/
@XmlElement
public Set<VoteJAXB> getVote() {
return voteJAXBSet;
}
/**
* @param voteJAXBSet the voteJAXBSet to set
*/
public void setVoteJAXBSet(Set<VoteJAXB> voteJAXBSet) {
this.voteJAXBSet = voteJAXBSet;
}
}
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
public class VoteJAXB implements Serializable {
private static final long serialVersionUID =
-3581860356522991089L;
private String phone;
private String firstName;
private String lastName;
private String email;
private String facebookUID;
/**
* @return the phone
*/
@XmlAttribute
public String getPhone() {
return phone;
}
/**
* @param phone the phone to set
*/
public void setPhone(String phone) {
this.phone = phone;
}
/**
* @return the firstName
*/
@XmlAttribute(name = "firstname")
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
@XmlAttribute(name = "lastname")
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the email
*/
@XmlAttribute
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the facebookUID
*/
@XmlAttribute(name = "facebookid")
public String getFacebookUID() {
return facebookUID;
}
/**
* @param facebookUID the facebookUID to set
*/
public void setFacebookUID(String facebookUID) {
this.facebookUID = facebookUID;
}
}
Any ideas what is going wrong?
-Jonathan