忽略 class 上注释的 Jackson @JsonFilter
Ignore Jackson @JsonFilter annotated on class
我有下面的代码...我希望 Jackson 在下面的 else 条件下忽略 class 上的 @JsonFilter。我只希望它在我有一些过滤器的情况下考虑 @JsonFilter。
@JsonFilter("filter")
public class Test {
}
if (filters != null)
mapper.writer(filters).writeValue(jsonGenerator,
response.getOriginalResponse());
else
mapper.writeValue(jsonGenerator, response.getOriginalResponse());
我这样做是为了在 else 条件下绕过过滤器。
SimpleFilterProvider dummy = new SimpleFilterProvider();
dummy.setFailOnUnknownId(false);
mapper.writer(dummy).writeValue(jsonGenerator,
response.getOriginalResponse());
如果您应用 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”) 并创建一个 class 等效于 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 都可以正常工作
我有下面的代码...我希望 Jackson 在下面的 else 条件下忽略 class 上的 @JsonFilter。我只希望它在我有一些过滤器的情况下考虑 @JsonFilter。
@JsonFilter("filter")
public class Test {
}
if (filters != null)
mapper.writer(filters).writeValue(jsonGenerator,
response.getOriginalResponse());
else
mapper.writeValue(jsonGenerator, response.getOriginalResponse());
我这样做是为了在 else 条件下绕过过滤器。
SimpleFilterProvider dummy = new SimpleFilterProvider();
dummy.setFailOnUnknownId(false);
mapper.writer(dummy).writeValue(jsonGenerator,
response.getOriginalResponse());
如果您应用 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”) 并创建一个 class 等效于 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 都可以正常工作