将JSON字符串数组传递给CXF Rest方法

时间:2017-01-17 10:10:53

标签: json cxf cxfrs

我需要将字符串/字符串数组列表传递给使用Apache CXF实现的restful服务方法。我可以通过将ArrayList包装在用JAXB注释装饰的pojo类中来实现这一点。

我应该只为一个实例变量创建一个绑定类吗?我的方法只需要一个参数(即字符串数组)。我不能将JSON数组直接绑定到数组或arraylist,而不是绑定在另一个类中吗?

请求JSON:

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

POJO班

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

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

工作方法

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

我在寻找什么

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

投掷低于错误

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

1 个答案:

答案 0 :(得分:0)

如果直接传递JSON数组[178,304,272]

,则可以完美地使用您的方法
@POST
@Path("cancelThese")
@Consumes(MediaType.APPLICATION_JSON)
public void cancelThese(List<String> ids) throws WebServiceFault {
        //---- implementation
}

使用CXF 3.1.6和Jackson 2.4.2进行测试