users@jersey.java.net

[Jersey] exception unmarshaling a JSON object

From: Christopher Schmidt <fakod666_at_googlemail.com>
Date: Wed, 15 Dec 2010 19:25:39 +0100

Hi all, I have a problem with unmarshaling a JSON object.


The REST Client code is:

public static void main(String [] args) throws Exception {

        Client c = Client.create();
        URI baseUri = UriBuilder.fromUri("http://localhost:7474/db/data/
").build();
        WebResource webResource = c.resource(baseUri);

        c.addFilter(new LoggingFilter());

        webResource.path("node/3/traverse/path")
                .accept(MediaType.APPLICATION_JSON)
                .type(MediaType.APPLICATION_JSON)
                //.post(JSONArray.class, "{\"order\":\"depth first\", \"max
depth\": 1}");
                //.post(TraversePath[].class, "{\"order\":\"depth first\",
\"max depth\": 1}");
                .post(GetPath.class, "{\"order\":\"depth first\", \"max
depth\": 1}");
    }

The JSON object returned is:

[ {
  "start" : "http://localhost:7474/db/data/node/3",
  "nodes" : [ "http://localhost:7474/db/data/node/3", "
http://localhost:7474/db/data/node/1" ],
  "length" : 1,
  "relationships" : [ "http://localhost:7474/db/data/relationship/6" ],
  "end" : "http://localhost:7474/db/data/node/1"
}, {
  "start" : "http://localhost:7474/db/data/node/3",
  "nodes" : [ "http://localhost:7474/db/data/node/3", "
http://localhost:7474/db/data/node/2" ],
  "length" : 1,
  "relationships" : [ "http://localhost:7474/db/data/relationship/2" ],
  "end" : "http://localhost:7474/db/data/node/2"
} ]


The first post statement using JSONArray works fine.

The second one using TraversePath that is defined as:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
class TraversePath {
  var start:String = _
  var nodes:Array[String] = _
  var length:Integer = _
  var relationships:Array[String] = _
  var end:String = _
}

Its Scala code, that doesn't matter, throws the same error with Java object:

Exception in thread "main" javax.ws.rs.WebApplicationException:
javax.xml.bind.UnmarshalException
 - with linked exception:
[javax.xml.bind.UnmarshalException: unexpected element (uri:"",
local:"start"). Expected elements are <{}traversePath>]
at
com.sun.jersey.core.provider.jaxb.AbstractListElementProvider.readFrom(AbstractListElementProvider.java:233)
at
com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:552)
at
com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:505)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:563)


The 3rd using an object GetPath with an array of TraversePath throws:


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.get(ArrayList.java:324)
at
com.sun.jersey.json.impl.reader.JsonXmlStreamReader.valueRead(JsonXmlStreamReader.java:165)
at
com.sun.jersey.json.impl.reader.JsonXmlStreamReader.readNext(JsonXmlStreamReader.java:330)
at
com.sun.jersey.json.impl.reader.JsonXmlStreamReader.readNext(JsonXmlStreamReader.java:178)
at
com.sun.jersey.json.impl.reader.JsonXmlStreamReader.next(JsonXmlStreamReader.java:448)
at
com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:192)
at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:360)
at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:339)
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:109)
at
com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:552)

Any advice?

Please add my email as CC because I am not yet subscribed to the list.

Thanks...

-- 
Christopher
twitter: @fakod
blog: http://blog.fakod.eu