Jackson ObjectMapper - specify serialization order of object properties

后端 未结 9 1597
梦毁少年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:31

    There is an easier way in Spring Boot by specifying a property (in application.properties for example:

    spring.jackson.mapper.sort_properties_alphabetically=true
    
    0 讨论(0)
  • 2020-11-27 18:33

    From the Jackson Annotations documentation:

    // ensure that "id" and "name" are output before other properties
    @JsonPropertyOrder({ "id", "name" })
    
    // order any properties that don't have explicit setting using alphabetic order
    @JsonPropertyOrder(alphabetic=true)
    
    0 讨论(0)
  • 2020-11-27 18:34

    In Spring Boot you can add this behaviour globally by adding the following to your Application entry point class:

      @Bean
      public Jackson2ObjectMapperBuilder objectMapperBuilder() {
    
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        builder.featuresToEnable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
    
        return builder;
      }
    
    0 讨论(0)
提交回复
热议问题