Jackson's @JsonView annotation does not work

后端 未结 1 1717
广开言路
广开言路 2020-12-22 01:33

I annotated class User with @JsonView and when it returned I see all fields even than that not contains in view class. Here is my class

@Entity
@Table(name =         


        
相关标签:
1条回答
  • 2020-12-22 02:27

    I've spend a some debugging time with the same issue. Results are:

    • all fields are included by default if you do not change this behavior (see BeanSerializerFactory.processViews). To change default do:

      ObjectMapper mapper = new ObjectMapper();
      mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
      
    • fields, marked by @JsonView omitted in result if controller method annotated with OTHER @JsonView (see FilteredBeanPropertyWriter.serializeAsField)

    So for your use-case do not change default settings, annotate Long userID by @JsonView and getUser by any other (not the same) View.

    Code com\fasterxml\jackson\core\jackson-databind\2.8.4\jackson-databind-2.8.4-sources.jar!\com\fasterxml\jackson\databind\MapperFeature.java

       * Feature is enabled by default.
       */
       DEFAULT_VIEW_INCLUSION(true)
    

    is contradicted of blog https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring, so I have to look at code closer.

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