Redis
 sql >> Teknologi Basis Data >  >> NoSQL >> Redis

jackson deserialize objek dengan daftar antarmuka pegas

Saya pikir Anda perlu menambahkan deserializer khusus

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

}

Ini json saya

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

Kemudian di bagian atas POJO Anda

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

Ini dia ujiannya

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

}




  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. ScaleGrid Hosting Menambahkan Dukungan untuk Cluster Redis™ yang Sangat Tersedia dengan Sharding Otomatis

  2. Menginstal dan Mengonfigurasi Redis di Ubuntu

  3. Redis `SCAN`:bagaimana menjaga keseimbangan antara kunci baru yang mungkin cocok dan memastikan hasil akhirnya dalam waktu yang wajar?

  4. Cara men-cache objek besar menggunakan Redis cache

  5. Bagaimana membuat Redis memilih kebijakan penggusuran LRU hanya untuk beberapa kunci?