2012-03-15 11 views
5

Posso inserire correttamente i dati XML nel mio servizio, ma, provando a fare lo stesso usando JSON, sto fallendo. Il POJO è:Come inoltrare la richiesta JSON a un servizio REST di Jersey?

@XmlRootElement 
public class Address { 
    String city; 
    String zip; 
    //Getters & setters... 
} 

La risorsa servizio è:

@POST 
@Produces("application/json") 
public Address fix(Address a) { 
    return a; 
} 

Sto facendo un POST come segue:

POST /AcmeWeb/svc/simple HTTP/1.1 
Content-Length: 30 
Content-Type: application/json; charset=UTF-8 

{"city":"Miami","zip":"33130"} 

Il server risponde con un 400 Bad Request. Ho setacciato Internet ma non ho trovato alcun buon esempio di pubblicazione di JSON. Qualsiasi aiuto è apprezzato. Grazie.

risposta

4

Aggiungi annotazione @Consumes("application/json") al tuo metodo fix().

Update: Questo funziona per me: il metodo

risorse: classe

@POST 
@Produces("application/json") 
@Consumes("application/json") 
public Address post(Address addr) { 
    return addr; 
} 

Indirizzo:

@XmlRootElement 
public class Address { 
    public String city; 
    public String zip; 
} 

Questa è la richiesta che ho mando:

Accept application/json 
Content-Type application/json; charset=UTF-8 

{"city":"Miami","zip":"33130"} 

Questo è quello che io ottenere come risposta:

{"city":"Miami","zip":"33130"} 
+0

Questo non ha aiutato. Ho anche aggiunto un'intestazione Accept: Accettare: application/json, text/javascript, */*; q = 0,01 – RajV

+0

È strano, funziona per me. Ho aggiornato la mia risposta su come l'ho implementata. –

+0

Controllare il log del server per vedere se c'è qualche traccia di stack sul lato server. –

1

Sembra esserci un problema con il modulo EclipseLink (Eclipse Persistenza Servizi - 2.3.2.v20111125-r10461) in bundle nella distribuzione WLS:

javax.ws.rs.WebApplicationException: javax.xml.bind.UnmarshalException 
- with linked exception: 
[Exception [EclipseLink-25004] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.XMLMarshalException 
Exception Description: An error occurred unmarshalling the document 
Internal Exception: java.util.NoSuchElementException] 
    at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:113) 
    at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:474) 
    at com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider$EntityInjectable.getValue(EntityParamDispatchProvider.java:123) 
    at com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:46) 
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:153) 
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:183) 
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75) 
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288) 
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) 
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) 
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) 
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469) 
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(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(ServletContainer.java:537) 
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:844) 
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:242) 
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:216) 
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:132) 
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:352) 
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:235) 
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3284) 
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3254) 
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321) 
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120) 
    at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57) 
    at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2163) 
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2089) 
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2074) 
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1512) 
    at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:254) 
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256) 
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:221) 
Caused by: javax.xml.bind.UnmarshalException 
- with linked exception: 
[Exception [EclipseLink-25004] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.XMLMarshalException 
Exception Description: An error occurred unmarshalling the document 
Internal Exception: java.util.NoSuchElementException] 
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.handleXMLMarshalException(JAXBUnmarshaller.java:827) 
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:407) 
    at com.sun.jersey.json.impl.BaseJSONUnmarshaller.unmarshalJAXBElementFromJSON(BaseJSONUnmarshaller.java:108) 
    at com.sun.jersey.json.impl.BaseJSONUnmarshaller.unmarshalFromJSON(BaseJSONUnmarshaller.java:97) 
    at com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider.readFrom(JSONRootElementProvider.java:125) 
    at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:111) 
    ... 35 more 
Caused by: Exception [EclipseLink-25004] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.XMLMarshalException 
Exception Description: An error occurred unmarshalling the document 
Internal Exception: java.util.NoSuchElementException 
    at org.eclipse.persistence.exceptions.XMLMarshalException.unmarshalException(XMLMarshalException.java:95) 
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.convertSAXException(SAXUnmarshaller.java:842) 
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:830) 
    at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:602) 
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:399) 
    ... 39 more 
Caused by: java.util.NoSuchElementException 
    at com.sun.jersey.json.impl.reader.JsonReaderXmlEvent.getAttributeValue(JsonReaderXmlEvent.java:147) 
    at com.sun.jersey.json.impl.reader.JsonXmlStreamReader.getAttributeValue(JsonXmlStreamReader.java:655) 
    at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader$IndexedAttributeList.getValue(XMLStreamReaderReader.java:312) 
    at org.eclipse.persistence.oxm.record.UnmarshalRecord.startElement(UnmarshalRecord.java:648) 
    at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parseEvent(XMLStreamReaderReader.java:108) 
    at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parse(XMLStreamReaderReader.java:81) 
    at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parse(XMLStreamReaderReader.java:71) 
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:818) 
    ... 41 more 

Come soluzione alternativa è possibile creare un file jaxb.properties nel pacchetto in cui le classi JAXB (ad esempio Indirizzo) sono disposti con il seguente contenuto:

javax.xml.bind.context.factory=com.sun.xml.bind.v2.ContextFactory 

Questo dovrebbe dire al WLS utilizzare un'istanza della classe JAXBContext da JAXB RI (che è presente in WLS 12c anche) anziché quella da EclipseLink. Tieni presente che questa è solo una soluzione.

+0

Davvero causerà una 400 Bad Request (a differenza di un errore del server 500) o si tratta di un problema diverso? Vedo che hai proposto di taggare la domanda EclipseLink. – Rup

+0

Questo è ancora lo stesso problema - l'eccezione sopra sta causando una 400 Bad Request in Jersey. –

+1

In realtà, sembra un lettore xml Jersey Json che falsifica gli eventi XML per leggere JSON usando JAXB ha qualche problema (vedi l'ultima traccia dello stack). Cioè il problema potrebbe riguardare JSONXmlStreamReader di Jersey anziché MOXy. –

Problemi correlati