I am developing restful web services using JAX-RS and Apache CXF. Jackson is in the classpath but I am not sure if Apache CXF is using the same to serialise/deserialise classes. I am trying to POST an object (DTO) which is an inner static class and looks like this :
@XmlRootElement(name = "dto")
public static class DTO implements Comparable<DTO>, Cloneable{
private long id;
private String name;
private Collection<Class1> obj1;
private Collection<Class2> obj2;
@JsonDeserialize(as=SomeConcreteClass.class)
private Collection<String, Class3> obj3;
//getters and setters
}
Class1, Class2 and Class3 are also inner static classes within same outer class in which DTO class is. There classes including my DTO class are NOT annotated with any JSON annotation but only with XML annotations like @XmlRootElement and @XMLType. Should I annotate my DTO class and Class1, Class2 and Class3 with some JSON annotation like @JsonTypeName?
There is another inner class (within the same outer class) which contains a collection to the above DTO and looks like this :
@XmlRootElement(name = "dtos")
@XmlType(name = "DTOsType")
public static class DTOs{
@JsonProperty("dtos")
private Collection<DTO> values;
public DTOs() {
}
public DTOs(final Collection<DTO> values) {
this.values = values;
}
@JsonProperty("dtos")
@XmlElement(name = "dto")
public Collection<DTO> getValues() {
return values;
}
@JsonProperty("dtos")
public void setValues(final Collection<DTO> values) {
this.values = values;
}
}
obj1 and obj2 are TreeSet internally and obj3 is a TreeMap internally.
On server side my endpoint looks like this
@POST
@Path("{name}/endpoint")
@Consumes({ MediaType.APPLICATION_XML, MediaType.TEXT_XML,
MediaType.APPLICATION_JSON })
@Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML,
MediaType.TEXT_XML, MediaType.APPLICATION_JSON })
public Response addDTO(@PathParam("name") String name, DTOs dtos) {
//code here
...........
...........
}
JSON input looks like the following
{
"dtos":
{
"dto":
{
"class1s":
{
"class1": [
{
.......
},
{
........
}
]},
"name": "somename",
"class3s":
{
"class3":
{
.........
}
},
"id": 555589024908,
}
}
Now when I send JSON in Request Body through Postman, I get the following error
Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT
token
at [Source: (org.apache.cxf.transport.http.AbstractHTTPDestination$1); line:
3, column: 1] (through reference chain: my DTO class ["dto"])
I have already gone through this :
Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
I am not sure how to write a wrapper or send JSON as string to my backend REST method. The problem is with deserialisation part which is handled by framework.
This service works well with XML input.
Any pointers?
Thanks!