忽略杰克逊@JsonFilter在课堂上注释

时间:2015-03-19 18:17:34

标签: jackson

我有下面的代码......我希望Jackson在下面的else条件中忽略一个类的@JsonFilter。我只想在我有一些过滤器的情况下考虑@JsonFilter。

@JsonFilter("filter")
public class Test {

}


  if (filters != null)
        mapper.writer(filters).writeValue(jsonGenerator,
                response.getOriginalResponse());
    else
        mapper.writeValue(jsonGenerator, response.getOriginalResponse());

2 个答案:

答案 0 :(得分:1)

我这样做是为了在else条件下绕过过滤器。

     SimpleFilterProvider dummy = new SimpleFilterProvider();
        dummy.setFailOnUnknownId(false);
        mapper.writer(dummy).writeValue(jsonGenerator,
                response.getOriginalResponse());

答案 1 :(得分:0)

如果您应用 JsonFilter,那么在每个响应中都需要过滤器。我正在提供一个解决方案,说明我如何在某些情况下避免使用 @JsonFilter 并在某些情况下应用。

问题场景

 @Entity
@Table(name=“course")
@JsonFilter(“courseFilters")
public class CourseEntity implements Serializable{

    private static final long serialVersionUID = 1L;

     @Id
     @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Long id;

     @Column(name = "favourite_course")
     private boolean favouriteCourse;
    
     @Column(name = "email_address")
     private String emailAddress;
    
     @Column(name = "created_on")
     private LocalDateTime createdOn; 
     
     @Column(name = “course_group")
     private String courseGroup;
     
     @JsonProperty("Level")
     @Column(name = "level")
     private String level;
….
}
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class CourseResponse {
        private String status;
        private Object data;
…….
}

@GetMapping(“/filterCourses”)
    public ResponseEntity<CourseResponse> filterCourses() throws Exception {
        CourseResponse response = null;
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        SimpleBeanPropertyFilter simpleBeanFilter = SimpleBeanPropertyFilter.filterOutAllExcept(“emailAddress”,”courseGroup”,”level”);
      
          List<Course> courses = courseService.findAll();
          
            FilterProvider filterProvider = new SimpleFilterProvider().addFilter("courseFilters",simpleBeanFilter);
            
            ObjectWriter writer = mapper.writer(filterProvider);
            String writeValueAsString = writer.writeValueAsString(courses);
            List<Course> result = mapper.readValue(writeValueAsString, List.class);

                response = new CourseResponse(HttpStatus.OK.name(), result);
       return new ResponseEntity<>(response, HttpStatus.OK);
        
    }
@GetMapping(“/courses”)
    public ResponseEntity<CourseResponse> courses() throws Exception {
        CourseResponse response = null;
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        SimpleBeanPropertyFilter simpleBeanFilter = SimpleBeanPropertyFilter.filterOutAllExcept(“emailAddress”,”courseGroup”,”level”);
      
          List<Course> courses = courseService.findAll();

                response = new CourseResponse(HttpStatus.OK.name(), result);
        return new ResponseEntity<>(response, HttpStatus.OK);       
    }

从上面的代码片段中,/filterCourses 可以正常工作并提供过滤后的数据,但未过滤的 api /courses 将显示错误,提示没有配置 id 'courseFilters'(类型 java.lang.String)的过滤器(通过参考链:java.util.ArrayList[0])

解决方法

从 CourseEntity 中删除 @JsonFilter(“courseFilters”) 并创建一个等效于 CourseEntity 的类,例如 CourseMixIn 并在其上应用相同的 @JsonFilter(“courseFilters”) 并将以下代码段添加到 /filterCourses api

mapper.addMixIn(Course.class, CourseMixIn.class);

查看下面的代码片段以获得完整的源代码

@JsonFilter(“courseFilters")
public class CourseMixIn implements Serializable{

    private static final long serialVersionUID = 1L;

     private Long id;

     private boolean favouriteCourse;
    
     private String emailAddress;
    
     private LocalDateTime createdOn; 
     
     private String courseGroup;
     
     private String level;
….
}
@GetMapping(“/filterCourses”)
    public ResponseEntity<CourseResponse> filterCourses() throws Exception {
        CourseResponse response = null;
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
       mapper.addMixIn(Course.class, CourseMixIn.class);
        SimpleBeanPropertyFilter simpleBeanFilter = SimpleBeanPropertyFilter.filterOutAllExcept(“emailAddress”,”courseGroup”,”level”);
      
          List<Course> courses = courseService.findAll();
          
            FilterProvider filterProvider = new SimpleFilterProvider().addFilter("courseFilters",simpleBeanFilter);
            
            ObjectWriter writer = mapper.writer(filterProvider);
            String writeValueAsString = writer.writeValueAsString(courses);
            List<Course> result = mapper.readValue(writeValueAsString, List.class);

                response = new CourseResponse(HttpStatus.OK.name(), result);
       return new ResponseEntity<>(response, HttpStatus.OK);
        
    }

现在 api /filterCourses 和 /courses 都可以正常工作