JSON Parsing with JAX-RS
I am creating a REST API with JAX-RS. I have a POST that uses a JSON element:
Element is a class:
@XmlRootElement
public class EventData{
public long start;
public long end;
public Collection<Person> persons;
}
I have a way like this:
@POST
@Consumes({MediaType.APPLICATION_JSON})
public Response transactionRequest(EventData insert){
....}
if i send a JSON string for EventData
, it works fine, but if i switch to:
@POST
@Consumes({MediaType.APPLICATION_JSON})
public Response transactionRequest(ArrayList<EventData> insert){
....}
and send a JSON string like this "{eventData:[{start:x,end:y,persons:[....]}]"
, it will build objects ArrayList
and EventData
, but object variables EventData
will null
.
Does anyone help?
source to share
You need to send a JSON array of JSON objects that represent your class EventData
.
The sample you provided is not such a JSON array, but a JSON object with a single property named "eventData" containing a JSON array.
Try something like this (based on your class EventData
):
[
{ "start":1, "end":2, "persons":[] },
{ "start":3, "end":4, "persons":[] }
]
Note that your class is not mentioned EventData
because JSON has no concept of named types - it's just objects and arrays of objects; only object properties have names.
source to share