我有一堂简单的课
public class AuthenticationToken {
public String token;
public AuthenticationToken(String token) {
this.token = token;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
使用杰克逊,我试图像这样将字符串映射到此对象
private String input = "{\"token\":\"adf\"}";
@Test
public void whenJsonString_ThenCreateAuthenticationObject() throws IOException {
ObjectMapper jsonMapper = new ObjectMapper();
AuthenticationToken tokenObject = jsonMapper.readValue(input,AuthenticationToken.class);
assertThat(tokenObject).isNotNull();
}
但它引发以下异常
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `foo.AuthenticationToken` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (String)"{"token":"adf"}"; line: 1,column: 2]
我试图将AuthenticationToken中的属性注释为@JsonProperty,但这也导致了此异常.
最佳答案