如何将JSON请求发布到Jersey REST服务?

如何将JSON请求发布到Jersey REST服务?,jersey,eclipselink,jax-rs,moxy,Jersey,Eclipselink,Jax Rs,Moxy,我可以成功地将XML数据发布到我的服务中,但是,尝试使用JSON进行同样的操作是失败的。POJO是: @XmlRootElement 公共课堂演讲{ 字符串城市; 拉链; //接球手和接球手。。。 } 服务资源是: @POST @生成(“应用程序/json”) 公共广播定位(地址a){ 返回a; } 我正在做一个如下的帖子: POST /AcmeWeb/svc/simple HTTP/1.1 Content-Length: 30 Content-Type: application/json;

我可以成功地将XML数据发布到我的服务中,但是,尝试使用JSON进行同样的操作是失败的。POJO是:

@XmlRootElement
公共课堂演讲{
字符串城市;
拉链;
//接球手和接球手。。。
}
服务资源是:

@POST
@生成(“应用程序/json”)
公共广播定位(地址a){
返回a;
}
我正在做一个如下的帖子:

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

{"city":"Miami","zip":"33130"}
服务器正在响应400个错误请求。我浏览了互联网,但没有找到发布JSON的好例子。感谢您的帮助。谢谢。

向您的
fix()
方法添加
@Consumes(“应用程序/json”)
注释

更新:这对我有用:

资源方法:

@POST
@Produces("application/json")
@Consumes("application/json")
public Address post(Address addr) {
    return addr;
}
地址类别:

@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"}

WLS发行版中捆绑的EclipseLink模块(Eclipse持久性服务-2.3.2.v20111125-r10461)似乎存在问题:

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类(例如地址)所在的包中创建一个
jaxb.properties
文件,其中包含以下内容:

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

这应该告诉WLS使用来自JAXB RI的
JAXBContext
类的实例(它也存在于WLS 12c中),而不是EclipseLink中的实例。请记住,这只是一个解决办法。

这没有帮助。我甚至添加了一个Accept头:Accept:application/json,text/javascript,/;q=0.01这很奇怪,它对我有效。我已经更新了我的答案,说明了我是如何实现它的。检查服务器日志,看看服务器端是否有堆栈跟踪。嗨,Martin,基本上,WebLogic 12c附带的Jersey版本中似乎存在缺陷。我无法执行表单POST并返回JSON。只有GET可以返回JSON。缺陷似乎与POST方法有关。我不知道该说什么。你的建议应该行得通。有时间时,我将与Tomcat/Jersey核实。现在,在WebLogic中,我做了一个变通,并发送了一个带有URL参数的GET请求。这真的会导致400个错误请求(而不是500个服务器错误)还是另一个问题?我知道您已经建议标记问题EclipseLink。这仍然是同一个问题-上面的异常在Jersey中导致了400个错误请求。实际上,看起来Jersey Json xml阅读器使用JAXB伪造xml事件来读取Json有一些问题(参见最后的堆栈跟踪)。也就是说,问题可能与Jersey的JsonXmlStreamReader有关,而不是与MOXy有关。以下是Jersey JsonXmlStreamReader问题的链接: