2016-03-01 12 views
6

Potrzebuję zapisać i załadować obiekty z Redis.jackson deserialize obiekt z listą interfejsu sprężyny

Obiekt zawiera listę GrantedAuthority (między innymi), który jest interfejsem:

public class UserAccountAuthentication implements Authentication { 
    private List<GrantedAuthority> authorities; 
    private boolean authenticated = true; 
    ... 
} 

Jackson powodzeniem serializuje obiektu, ale nie deserializowania go z następującym wyjątkiem:

abstract types can only be instantiated with additional type information 

I wiem, że mogę określić typ, dodając:

@JsonTypeInfo(

B ut Nie mogę tego zrobić w tym przypadku, ponieważ GrantedAuthority jest interfejsem Spring i nie mogę go zmienić.


szeregowej json jest:

{ 
"authorities": [ 
    { 
     "authority": "ROLE_NORMAL_USER" 
    } 
], 
"authenticated": true, 
"securityToken": { 
    "expiration": 1458635906853, 
    "token": "sxKi3Pddfewl2rgpatVE7KiSR5qGmhpGl0spiHUTLAAW8zuoLFE0VLFYcfk72VLnli66fcVmb8aK9qFavyix3bOwgp1DRGtGacPI", 
    "roles": [ 
     "ROLE_NORMAL_USER" 
    ], 
    "expired": false, 
    "expirationDateFormatted": "2016-03-22 08:38:26.853 UTC" 
}, 
"name": "admin", 
"expired": false 

}


abstrakcyjnej GrantedAuthority został napełniony tylko SimpleGrantedAuthority.

więc próbowałem:

objectMapper.registerSubtypes(SimpleGrantedAuthority.class); 

i nadal nie ma szczęścia.

+0

Widziałeś jeśli pobierające i ustawiające są napisane poprawnie? –

+0

nie ma żadnych obiektów pobierających i ustawiających, jest to prywatna lista, z którą obiekt współpracuje wewnętrznie. – Mike

Odpowiedz

5

myślę, że trzeba dodać niestandardowy Deserializator

public class UserAccountAuthenticationSerializer extends JsonDeserializer<UserAccountAuthentication> { 

@Override 
public 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; 
} 

}

To jest mój json

{"authenticated":true,"authorities":[{"authority":"role1"},{"authority":"role2"}],"details":null,"principal":null,"credentials":null,"name":null} 

Następnie na górze POJO

@JsonDeserialize(using = UserAccountAuthenticationSerializer.class) 
public class UserAccountAuthentication implements Authentication { 

Oto test

@Test 
public 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); 

}

+0

Podążyłem za twoim kodem krok po kroku i skompilowałem go, ale gdy jego działanie "deserializer" nigdy nie kończyło się tak, jak gdyby nigdy nie działało i otrzymuję ten błąd: org.codehaus.jackson.map.JsonMappingException: Nie można deserializować instancji com.coronet.online.security.GrantedAuthorityContainer z START_ARRAY tokena w [Źródło: [email protected]; linia: 1, kolumna: 2] (przez łańcuch referencyjny: com.coronet.online.security.UserAccountAuthentication ["organy"]) – Mike

+0

Czy możesz opublikować swój json, dodałem Jason używany w moim teście. –

+0

robi to teraz ... – Mike

Powiązane problemy