1. Inject ObjectMapper
2. registered class HistoricTaskInstance serialization and deserialization class HistoricTaskInstanceKeySerializer, HistoricTaskInstanceKeyDeSerializer
3. Modeule to the new registration in ObjectMapper.
class A {
//. 1. ObjectMapper injection, ObjectMapper is noted that overall spring. @Autowired Private ObjectMapper ObjectMapper; public void setKeyProcessor () {
SM = new new SimpleModule SimpleModule ();
// register serialization, deserialization class.
sm.addKeySerializer (HistoricTaskInstance.class, new new HistoricTaskInstanceKeySerializer ());
sm.addKeyDeserializer (HistoricTaskInstance.class, new new HistoricTaskInstanceKeyDeSerializer ());
// registered to ObjectMapper in.
objectMapper.registerModule (sm);
}
}
static class HistoricTaskInstanceKeySerializer extends JsonSerializer<HistoricTaskInstance> {
private final ObjectMapper objectMapper = new ObjectMapper();
@Override
public void serialize(HistoricTaskInstance historicTaskInstance, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
StringWriter writer = new StringWriter();
objectMapper.writeValue(writer, historicTaskInstance);
jsonGenerator.writeFieldName(writer.toString());
}
}
static class SimpleObjectKeyDeSerializer extends KeyDeserializer {
private final ObjectMapper objectMapper = new ObjectMapper();
@Override
public HistoricTaskInstance deserializeKey(String key, DeserializationContext deserializationContext) throws IOException {
return objectMapper.readValue(key, HistoricTaskInstance.class);
}
}
class HistoricTaskInstance{
private String name;
private String address;
}