JAX-RS – 在apache CXF中没有根节点的JSON

如果我们在REST响应中返回集合对象,那么JSON(它将根元素节点作为集合对象名称 – 在这种情况下是雇员)将采用以下格式:

{ "employees": [{ "id": "1", "name": "employee name1", "company": "ABC Company" }, { "id": "2", "name": "employee name2", "company": "XYZ Company" }] 

}

这是我们在应用程序上下文中的JsonProvider配置的一个截断器

       @XmlRootElement(name="emps") public class EmpList{ private List employees; //setter and getter methods } @XmlRootElement(name="emp") public class Emp{ private int id; private Sting name; private String company; //setter and getter methods } 

我不希望JSON响应中的Collection对象根元素节点。 输出应采用以下格式。 我正在使用Apache CXF框架进行rest服务。

  { [{ "id": "1", "name": "employee name1", "company": "ABC Company" }, { "id": "2", "name": "employee name2", "company": "XYZ Company" }] 

}

我们使用默认的cxf JsonProvider(Jettison)

请建议任何解决方案。 提前致谢。

您可以通过自定义provider来使用droproot element属性进行配置

      

您也可以使用自定义JAXBElement进行配置,请在此处查看

     {http://numbers}number index