2012-03-15 77 views
5

我可以將XML數據成功上傳到我的服務,但是,嘗試做使用JSON同樣是失敗。該POJO是:如何將JSON請求發佈到Jersey REST服務?

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

服務資源是:

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

我做一個POST如下:

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

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

服務器與400錯誤的請求響應。我搜索了互聯網,但沒有發現發佈JSON的好例子。任何幫助表示讚賞。謝謝。

回答

4

添加@Consumes("application/json")註釋您fix()方法。

更新:這工作對我來說:

資源的方法:

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

Address類:

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

這是我送的要求:

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

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

這就是我得到的迴應:

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

沒有幫助。我甚至增加了一個接受頭: 接受:應用/ JSON,文本/ JavaScript中,*/*; q = 0.01 – RajV 2012-03-15 17:43:19

+0

這很奇怪,它適用於我。我已經更新了我的答案和我如何實現它。 – 2012-03-15 18:44:49

+0

檢查服務器日誌以查看服務器端是否存在任何堆棧跟蹤。 – 2012-03-15 18:52:04

1

似乎有與的EclipseLink模塊(Eclipse持久服務 - 2.3.2.v20111125-r10461)的問題在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 

作爲解決方法,您可以在包中創建一個jaxb.properties文件,您的JAXB類(例如地址)位於具有以下內容:

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

這應該告訴WLS使用的,而不是從EclipseLink的一個從JAXB RI(這也是目前在WLS 12C的JAXBContext類的實例)。請記住,這只是一個解決方法。

+0

這真的會導致400錯誤請求(而不是500服務器錯誤)還是這是一個不同的問題?我看到你提出了標記EclipseLink的問題。 – Rup 2012-03-20 13:28:55

+0

這仍然是一個問題 - 上述異常導致澤西島發生400錯誤請求。 – 2012-03-20 14:29:34

+1

實際上,看起來像Jersey Json xml讀取器,它使用JAXB取消XML事件來讀取JSON有一些問題(請參閱最後一個堆棧跟蹤)。即這個問題可能與Jersey的JsonXmlStreamReader而不是MOXy有關。 – 2012-03-20 15:23:45

相關問題