How to make Jersey/Jackson serialize empty list; single element list as an array

前端 未结 4 1853
青春惊慌失措
青春惊慌失措 2021-01-18 13:14

Using Jersey and Jackson to create a REST interface, how do I get List fields to be serialized as a list when there are 0 or 1 elements in them. For example:



        
4条回答
  •  耶瑟儿~
    2021-01-18 13:42

    Yes, we also faced the same issue. Jackson cannot serialize list with 0 or single element to json array. So we tried to use Json's ObjectMapper to convert POJO object to String. It will serialize java List to json array irrespective of number of elements in List (0 or 1 or > 0). The code would look like :

     request = new ObjectMapper().writeValueAsString(pojo);
    

    where request is of type Object. This will not affect response. You can give a try.

提交回复
热议问题