【发布时间】:2017-12-21 06:35:19
【问题描述】:
第一次使用 Jackson 过滤器时,我很惊讶地发现它根本不起作用。这是我刚刚写的测试代码:
@Test
public void testJsonSerialization() throws JsonProcessingException {
Principal princi = Principal.builder().fullName("John Smith").role(Role.USER).build().setAccountType(CHILD)
.setClassAttended(Classes.C10);
SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter.filterOutAllExcept("fullName");
FilterProvider provider = new SimpleFilterProvider().addFilter("bareBonesFilter", filter);
JsonFilter jFilter = Principal.class.getAnnotation(JsonFilter.class);
assertNotNull(jFilter); // passes
assertEquals("bareBonesFilter", jFilter.value()); // passes
provider.findPropertyFilter("bareBonesFilter", princi); // doesn't throw exception, therefore a filter was found
ObjectWriter writer = (new ObjectMapper()).setSerializationInclusion(Include.NON_ABSENT)
.enable(SerializationFeature.INDENT_OUTPUT).writer(provider);
String json = writer.writeValueAsString(princi);
assertFalse(json.contains("accountType")); //this fails
}
使用杰克逊 2.7.1。
我错过了什么?我需要在某处添加一些特殊配置以启用过滤器吗?
更新 1
按照@StaxMan 的建议,我用 2.7.9 和 2.9.4 尝试了上述代码,但都没有成功。
然后,我没有使用Principal 类(它变得庞大而复杂),而是使用更简单的SgEmailInfo 类(它只是一个数据持有者)尝试了上面的代码,并且过滤按预期工作!
@Data
@Accessors(chain = true)
@JsonFilter("bareBonesFilter")
public class SgEmailInfo {
private long created;
private String email;
private String reason;
private String status;
}
@Data 和 @Accessors 是 Lombok 注释,用于自动生成一些代码。 Principal 类也使用这些注解以及其他 Lombok 注解:
@Entity //Objectify annotation
@Cache(expirationSeconds = 600) //Objectify annotation
@Data
@EqualsAndHashCode(of = "id", callSuper = false) // Lombok
@ToString(callSuper = true, doNotUseGetters = true, of = { "id", "firstName", "middleName",
"lastName", "profileName", "roles" }) // Lombok
@Accessors(chain = true)
@JsonFilter("bareBonesFilter")
public class Principal extends AccountEntity<Principal, EmailPrincipal>
implements Serializable, ProfileShort, ProfileMedium
我还在Principal 的许多属性和吸气剂上使用@JsonIgnore 注释。
Principal 的构造函数上还有 @Builder 注释:
@Builder
private Principal(String fullName, @Singular List<Role> roles)
Lombok 生成的这些注释或代码是否会干扰过滤?
【问题讨论】:
-
首先要使用更现代的版本。在 2.7 中,有很多很多补丁(2.7.9),最新的一般是 2.9.4。
-
是的,Lombok 是...有点 PITA,因为它以一种可能无法与其他库配合使用(或至少不会立即可见)的方式增加注释。