2016-03-01 9 views
6

मुझे redis से वस्तुओं को सहेजने और लोड करने की आवश्यकता है।जैक्सन वसंत के इंटरफ़ेस की सूची के साथ deserialize ऑब्जेक्ट

वस्तु (अन्य बातों के अलावा) GrantedAuthority की सूची होती है जो एक अंतरफलक है:

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

जैक्सन सफलतापूर्वक वस्तु को धारावाहिक लेकिन निम्न अपवादों के साथ यह deserialize करने में विफल रहता:

abstract types can only be instantiated with additional type information 

मैं पता है कि मैं जोड़कर टाइप निर्दिष्ट कर सकता हूं:

@JsonTypeInfo(

बी क्योंकि मैं इस मामले में ऐसा नहीं कर सकता क्योंकि GrantedAuthority वसंत का एक इंटरफेस है और मैं इसे बदल नहीं सकता।


धारावाहिक json है:

{ 
"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 

}


सार GrantedAuthority केवल SimpleGrantedAuthority से भरा है।

तो मैं करने की कोशिश की:

objectMapper.registerSubtypes(SimpleGrantedAuthority.class); 

और फिर भी प्रयास विफल।

+0

आप देखा यदि आपके getters और setters ठीक से लिखे गए हैं? –

+0

कोई गेटर्स नहीं हैं और यह एक निजी सूची है जो ऑब्जेक्ट आंतरिक रूप से काम करता है। – Mike

उत्तर

5

मैं आप एक कस्टम deserializer

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; 
} 

}

यह आपके POJO के शीर्ष पर तब मेरे json

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

है जोड़ने की जरूरत है लगता है

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

परीक्षा यहाँ है

@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

मैंने आपके कोड चरण-दर-चरण का पालन किया और इसे संकलित कर लिया, लेकिन जब यह "deserializer" चला रहा है तो कभी नहीं टूटता है जैसे कि यह कभी नहीं चलता है और मुझे यह त्रुटि मिल रही है: org.codehaus.jackson.map.JsonMappingException: उदाहरण का deserialize नहीं कर सकता com.coronet.online.security.GrantedAuthorityContainer START_ARRAY टोकन से बाहर [स्रोत: [email protected]; रेखा: 1, कॉलम: 2] (संदर्भ श्रृंखला के माध्यम से: com.coronet.online.security.UserAccount प्रमाणीकरण ["अधिकारियों"]) – Mike

+0

क्या आप अपना जेसन पोस्ट कर सकते हैं, मैंने जेसन को अपने परीक्षण में इस्तेमाल किया है। –

+0

अब यह कर रहा है ... – Mike

संबंधित मुद्दे