我认为您需要添加自定义解串器
public class UserAccountAuthenticationSerializer extends JsonDeserializer<UserAccountAuthentication> {@Overridepublic UserAccountAuthentication deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { UserAccountAuthentication userAccountAuthentication = new UserAccountAuthentication(); ObjectCodec oc = jsonParser.getCodec(); JsonNode node = oc.readTree(jsonParser); userAccountAuthentication.setAuthenticated(node.get("authenticated").booleanValue()); Iterator<JsonNode> elements = node.get("authorities").elements(); while (elements.hasNext()) { JsonNode next = elements.next(); JsonNode authority = next.get("authority"); userAccountAuthentication.getAuthorities().add(new SimpleGrantedAuthority(authority.asText())); } return userAccountAuthentication;}}
这是我的json
{"authenticated":true,"authorities":[{"authority":"role1"},{"authority":"role2"}],"details":null,"principal":null,"credentials":null,"name":null}然后在您的POJO顶部
@JsonDeserialize(using = UserAccountAuthenticationSerializer.class)public class UserAccountAuthentication implements Authentication {这是测试
@Testpublic void test1() throws IOException {UserAccountAuthentication userAccountAuthentication = new UserAccountAuthentication();userAccountAuthentication.setAuthenticated(true);userAccountAuthentication.getAuthorities().add(new SimpleGrantedAuthority("role1"));userAccountAuthentication.getAuthorities().add(new SimpleGrantedAuthority("role2"));String json1 = new ObjectMapper().writevalueAsString(userAccountAuthentication);UserAccountAuthentication readValue = new ObjectMapper().readValue(json1, UserAccountAuthentication.class);String json2 = new ObjectMapper().writevalueAsString(readValue);assertEquals(json1, json2);}



