How to wrap a List as top level element in JSON generated by Jackson

前端 未结 2 475
死守一世寂寞
死守一世寂寞 2020-12-16 04:58

I am running into a problem where I am trying to include a List as the root node, but I can\'t seem to be able to get this. Let me explain. Let\'s say we have a class \"Test

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-16 05:20

    I'd expect the basic idea to be something like:

    class UtilityClass {
        List listOfTestClasses;
        UtilityClass(List tests) {
            this.listOfTestClasses = tests;
        }
    }
    
    String utilityMethod(){
        List list = someService.getList();
        UtilityClass wrapper = new UtilityClass(list);
        new ObjectMapper().writeValueAsString(wrapper); 
    }
    

提交回复
热议问题