我正在用Jackson交换Jsob-B,我在使用@JsonbTypeDeserializer和@JsonbTypeSerializer时遇到问题,如下所述。
我有一个这样的实体:
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String login;
@JsonbTypeDeserializer(Decrypting.class)
@JsonbTypeSerializer(Encrypting.class)
private String password;
// getters and setter
}
还有一个测试是这样的:
@Test
public void whenDeserializingUsingJsonbTypeDeserializer() throws IOException {
String json = "{"login":"admin", "password":"yfv_ntl3_Nbrv0139tDwRQ"}";
Jsonb jsonb = JsonbBuilder.create();
User user = jsonb.fromJson(json, User.class);
Assert.assertEquals(user.getPassword(), "test-string");
}
运行测试时,Decryption类试图反序列化login字段,但是,正如您所看到的,只有password域具有注释。
有什么办法解决它吗?
更新新版本后,JsonbConfigProperties出现问题。FAIL_ON_UNNOWN_PROPERTIES,我切换到YasonConfig。FAIL_ON_UNNOWN_PROPERTIES,现在一切正常。
<dependency>
<groupId>jakarta.json.bind</groupId>
<artifactId>jakarta.json.bind-api</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>1.0.7</version>
</dependency>