RESTEasy - 解组JSON时的“意外元素”?

时间:2013-05-22 02:04:50

标签: java json rest exception resteasy

我正在为Apple的AppStore查找服务编写一个RESTEasy客户端,该服务返回一个表示查询结果的JSON对象。对于我的测试用例,JSON看起来像这样:

{

 "resultCount":1,

 "results": [
{
    "kind":"software",
     "features":["gameCenter",
     "iosUniversal"],
     "supportedDevices":["all"],
     "isGameCenterEnabled":true,

   ...<more stuff>...

}]

}

我创建了一个非常简单的JAXB对象,只是为了看看我是否可以使用我的RESTEasy客户端从服务中成功解组响应。我只是试图从响应中映射“resultCount”属性。它看起来像这样:

@Mapped
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class AppStoreLookupResponse implements Serializable {

    private static final long serialVersionUID = 7589102375949422744L;

    @XmlElement
    private int resultCount = -1;

    /**
     * Returns the resultCount
     * 
     * @return The resultCount
     */
    public int getResultCount() {
        return resultCount;
    }

    /**
     * Sets the resultCount
     * 
     * @param resultCount
     *            The resultCount to set
     */
    public void setResultCount(int resultCount) {
        this.resultCount = resultCount;
    }

}

但是,当我运行我的客户端时,我看到以下异常:

org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException: javax.xml.bind.UnmarshalException
 - with linked exception:
[com.sun.istack.internal.SAXParseException2; columnNumber: 0; unexpected element (uri:"", local:"resultCount"). Expected elements are <{}appStoreLookupResponse>]
    at org.jboss.resteasy.plugins.providers.jaxb.AbstractJAXBProvider.readFrom(AbstractJAXBProvider.java:86)
    at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:105)
    at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.read(GZIPDecodingInterceptor.java:37)
    at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:108)
    at org.jboss.resteasy.core.messagebody.ReaderUtility.doRead(ReaderUtility.java:111)
    at org.jboss.resteasy.client.core.BaseClientResponse.readFrom(BaseClientResponse.java:291)
    at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:255)
    at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:228)
    at org.jboss.resteasy.client.core.extractors.BodyEntityExtractor.extractEntity(BodyEntityExtractor.java:56)
    at org.jboss.resteasy.client.core.ClientInvoker.invoke(ClientInvoker.java:102)
    at org.jboss.resteasy.client.core.ClientProxy.invoke(ClientProxy.java:72)
    at $Proxy20.lookupByID(Unknown Source)
    at net.odyssi.mms.appstore.apple.test.AppleAppStoreLookupClientTest.testLookupByID(AppleAppStoreLookupClientTest.java:111)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

究竟是什么导致这个抛出异常?我过去没有看到过这样的错误,并且在找到这个问题的解决方案方面没有取得任何成功。非常感谢您给予的任何帮助!!

1 个答案:

答案 0 :(得分:2)

终于找到了答案!我将RestEASY库升级到2.3.5,并删除了Jettison提供商JAR,转而使用Jackson。这解决了我所有的问题。