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

前端 未结 4 1806
青春惊慌失措
青春惊慌失措 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:21

    I managed to solve JSON array "bug" in recent Jersey json library (v1.14 Sep 2012). Secret ingredient is JSONConfiguration and ContextResolver magic. See my following post it has a full code example, customized ContextResolver and rest Application class might be somewhat fuzzy logic in first look.

    How to serialize Java primitives using Jersey REST

    Primitives and zero or single-element List array are properly serialized to JSON document.

    0 讨论(0)
  • 2021-01-18 13:23

    I am pretty sure that you are not actually using Jackson ("POJO" variant of JSON serialization), since Jackson would not convert single-element arrays or lists to anything else. So you are probably using one of legacy output methods (like jettison); meaning that if you configure system to use POJO mapping it should just work.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-18 13:43

    I wrote a blog post ages ago about forcing Jersey to serialize single element arrays correctly, not sure if it's out-dated now (its from mid-2010!), but it might be of use.

    Note the blog comment from Brill Pappin on the blog demonstrating a different approach which means upgrading the Jettison library that you are using.

    In short you can write a custom JaxbContextResolver that looks a little like:

    @Provider
    @Component
    public class JAXBContextResolver implements ContextResolver {
    
        private JAXBContext context;
    
        public JAXBContextResolver() throws Exception {
            MappedBuilder builder = JSONConfiguration.mapped();
            builder.arrays("invite");
            builder.rootUnwrapping(true);
            this.context = new JSONJAXBContext(builder.build(), Payload.class);
        }
    
        public JAXBContext getContext(Class objectType) {
            return (Payload.class.equals(objectType)) ? context : null;
        }
    }
    

    For clarity, my payload class looked a little like

    @XmlRootElement(name = "response")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Payload {
    
        @XmlElement(name = "invite")
        List invites;
    
        ... etc.
    

    Regarding stopping Jackson serializing bean properties as null, see my previous answer here, about using annotations to change that behaviour.

    0 讨论(0)
提交回复
热议问题