キーオブジェクトマップのプロジェクトジャクソン・シーケンスで使用される春

1.注入するObjectMapper 
2.登録したクラスHistoricTaskInstanceのシリアライズとデシリアライズクラスHistoricTaskInstanceKeySerializer、HistoricTaskInstanceKeyDeSerializer
3 Modeule ObjectMapperで新規登録します。
クラスA { 
   //。1. ObjectMapper注射、ObjectMapperは、その全体的なスプリングに留意されたいです。 @Autowired
プライベートObjectMapper ObjectMapper; 公共 ボイドsetKeyProcessor(){    
    SM =新しい新SimpleModule SimpleModule(); 
    //シリアライズ、デシリアライズクラスを登録します。
    sm.addKeySerializer(HistoricTaskInstance.class、新しい新HistoricTaskInstanceKeySerializer());
    sm.addKeyDeserializer(HistoricTaskInstance.class、新しい新HistoricTaskInstanceKeyDeSerializer());
    //にObjectMapperに登録。
    objectMapper.registerModule(SM)。
} 
}

静的クラスHistoricTaskInstanceKeySerializerは<HistoricTaskInstance> {JsonSerializerを拡張
プライベート最終ObjectMapper objectMapper =新しいObjectMapper()。
@Override
公共ボイドシリアライズ(HistoricTaskInstance historicTaskInstance、JsonGenerator jsonGenerator、SerializerProvider serializerProviderが)にIOException {スロー
にStringWriterライター=新しいにStringWriterを()。
objectMapper.writeValue(作家、historicTaskInstance)。
jsonGenerator.writeFieldName(writer.toString())。
}
}


静的クラスSimpleObjectKeyDeSerializerはKeyDeserializer {延び
プライベート最終ObjectMapper objectMapper =新しいObjectMapperを();
@Override
公共HistoricTaskInstance deserializeKey(文字列キー、DeserializationContext deserializationContextは)にIOExceptionが{スロー
objectMapper.readValue(キー、HistoricTaskInstance.class)を返します。
}
}

クラスHistoricTaskInstance {
  プライベート文字列名。
プライベート文字列のアドレス。
}
 

 

おすすめ

転載: www.cnblogs.com/luoluoshidafu/p/10979018.html