Jackson ObjectMapper - specify serialization order of object properties

后端 未结 9 1596
梦毁少年i
梦毁少年i 2020-11-27 17:38

I\'m implementing a RESTful web service where user has to send a signed verification token along with the request so that I could ensure that the request has not been tamper

相关标签:
9条回答
  • 2020-11-27 18:12

    In Jackson 2.x, which you are probably using today, use:

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
    

    If you care about looks, you may also consider SerializationFeature.INDENT_OUTPUT as well.

    Note that you must serialize Maps or Objects for this to sort correctly. If you serialize a JsonNode for example (from readTree), that won't be properly indented.

    Example

    import com.fasterxml.jackson.databind.*;
    
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
    mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    
    String input = "{\"hello\": {\"cruel\" : \"world\"} }";
    Object pojo = mapper.readValue(input, Object.class);
    System.out.println(mapper.writeValueAsString(pojo));
    

    results in:

    {
      "hello" : {
        "cruel" : "world"
      }
    }
    
    0 讨论(0)
  • 2020-11-27 18:22

    You can use mix-in and specify the order of properties as you like:

    import com.fasterxml.jackson.annotation.JsonPropertyOrder;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Component;
    
    @Component
    public final class ObjectMapperUtils {
    
        private static final ObjectMapper MAPPER = new ObjectMapper();
    
        static {
            MAPPER.addMixIn(Object.class, IdFirst.class);
        }
    
        @Bean
        public ObjectMapper objectMapper() {
            return MAPPER;
        }
    
        @JsonPropertyOrder({"id", "...", "..."})
        private abstract static class IdFirst {}
    
    }
    
    0 讨论(0)
  • 2020-11-27 18:23

    Instead of using flag argument:

    objectMapper.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
    
    0 讨论(0)
  • 2020-11-27 18:25

    The annotations are useful, but can be a pain to apply everywhere. You can configure your whole ObjectMapper to work this way with

    Current Jackson versions: objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)

    Older Jackson versions: objectMapper.configure(SerializationConfig.Feature.SORT_PROPERTIES_ALPHABETICALLY, true);

    0 讨论(0)
  • 2020-11-27 18:27

    From Duncan McGregor's answer: Its better to use it like this:

    objectMapper.configure(SerializationConfig.Feature.SORT_PROPERTIES_ALPHABETICALLY, true);
    

    as MapperFeature is for XMLs and comes with jackson-databind which is not required...

    0 讨论(0)
  • 2020-11-27 18:29

    The following 2 ObjectMapper configuration are required:

    ObjectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
    or
    ObjectMapper.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)

    defines the property serialization order used for POJO fields
    Note: does not apply to java.util.Map serialization!

    and

    ObjectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true)
    or
    ObjectMapper.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)

    Feature that determines whether java.util.Map entries are first sorted by key before serialization


    Spring Boot config example (yaml):

    spring:
      jackson:
        mapper:
          SORT_PROPERTIES_ALPHABETICALLY: true
        serialization:
          ORDER_MAP_ENTRIES_BY_KEYS: true
    
    0 讨论(0)
提交回复
热议问题