mardi 29 avril 2014

Java - Casting JSON à ressource en appel reste - Stack Overflow


I'm trying to accept JSON input from a POST and automatically cast it to the resource class, but I keep getting the error:


java.lang.ClassCastException: com.foo.MyResource cannot be cast to java.util.Collection

I thought I annotated my class correctly, but I feel like I'm missing something obvious. Can anyone help me figure out why this code will not parse the below JSON into a MyResource object?


My service class contains this method:


@POST
@Path("/somepath/{user}")
@Consumes(MediaType.APPLICATION_JSON)
public String createUser(MyResource resource, @PathParam("user") String user){
return "Got resource " + resource + " for " + user;
}

The resource is as follows:


@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD)
public class MyResource {
public String foo;
public String bar;
public int baz;
public String toString(){
return foo + " " + bar + " " + baz;
} }

My request message:


Headers:
Content-Length: 92
Host: requestb.in
Cache-Control: no-cache
Accept-Encoding: gzip,deflate,sdch
Content-Type: application/json
Connection: close
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Accept: */*
Cookie: session=eyJyZWNlbnQiOlsiMTZwcXV1NDEiXX0.BiBTRw.qNeCDkVfl-4Xog8hLhYuJFSlEYg; _ga=GA1.2.1701494324.1396374352
X-Request-Id: c9bd3f19-f071-47ad-9adb-d98232a7e8fb
Origin: chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
Accept-Language: en-US,en;q=0.8

RAW BODY

[
{
"foo": "foo string"
},
{
"bar": "bar string"
},
{
"baz": 5
}
]



The object you send is not is the right format :


You should send


{
"foo": "foo string",
"bar": "bar string",
"baz": 5
}

In fact the object you are sending is an array [] of objects {}. That's why there is a cast problem.



I'm trying to accept JSON input from a POST and automatically cast it to the resource class, but I keep getting the error:


java.lang.ClassCastException: com.foo.MyResource cannot be cast to java.util.Collection

I thought I annotated my class correctly, but I feel like I'm missing something obvious. Can anyone help me figure out why this code will not parse the below JSON into a MyResource object?


My service class contains this method:


@POST
@Path("/somepath/{user}")
@Consumes(MediaType.APPLICATION_JSON)
public String createUser(MyResource resource, @PathParam("user") String user){
return "Got resource " + resource + " for " + user;
}

The resource is as follows:


@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD)
public class MyResource {
public String foo;
public String bar;
public int baz;
public String toString(){
return foo + " " + bar + " " + baz;
} }

My request message:


Headers:
Content-Length: 92
Host: requestb.in
Cache-Control: no-cache
Accept-Encoding: gzip,deflate,sdch
Content-Type: application/json
Connection: close
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Accept: */*
Cookie: session=eyJyZWNlbnQiOlsiMTZwcXV1NDEiXX0.BiBTRw.qNeCDkVfl-4Xog8hLhYuJFSlEYg; _ga=GA1.2.1701494324.1396374352
X-Request-Id: c9bd3f19-f071-47ad-9adb-d98232a7e8fb
Origin: chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
Accept-Language: en-US,en;q=0.8

RAW BODY

[
{
"foo": "foo string"
},
{
"bar": "bar string"
},
{
"baz": 5
}
]


The object you send is not is the right format :


You should send


{
"foo": "foo string",
"bar": "bar string",
"baz": 5
}

In fact the object you are sending is an array [] of objects {}. That's why there is a cast problem.


0 commentaires:

Enregistrer un commentaire