jsoncxfcxfrs

Passing JSON String array to CXF Rest Method


I need to pass a list of strings / array of strings to a restful service method implemented using Apache CXF. I could achieve this by wrapping the ArrayList in a pojo class decorated with JAXB annotations.

Should I create a binding class just for one instance variable? My method takes only one parameter (i.e. array of strings). Can't I bind JSON array directly to an array or arraylist, instead of binding in another class?

Request JSON:

    {"ids":[178,304,272]}

POJO class

@XmlRootElement(name = "CommonRequest")
public class WSRestCommonRequest {

private List<String> ids;
    //getter setter methods
}

working Method

@POST
@Path("cancelThese")
public void cancelThese(CommonRequest request) throws WebServiceFault {
    //---- implementation
}

What I am looking for

public void cancelThese(List<String> ids) throws WebServiceFault {
        //---- implementation
}

It is throwing below error

Headers: {exception=[Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

Solution

  • You can perfectly use your method if you pass directly the JSON array [178,304,272]

    @POST
    @Path("cancelThese")
    @Consumes(MediaType.APPLICATION_JSON)
    public void cancelThese(List<String> ids) throws WebServiceFault {
            //---- implementation
    }
    

    Tested with CXF 3.1.6 and Jackson 2.4.2