You should use Mix-in Annotations feature. For example, your POJO
classes could look like that:
class Root {
private Data data;
// getters/setters
}
class Data {
private String name;
// getters/setters
}
Now, you have to create MixIn
interface:
interface RootMixIn {
@JsonSerialize(using = DataSerializer.class)
Data getData();
}
Imagine, that custom serializer looks like this:
class DataSerializer extends JsonSerializer<Data> {
@Override
public void serialize(Data data, JsonGenerator generator, SerializerProvider provider) throws IOException, JsonProcessingException {
generator.writeStartObject();
generator.writeFieldName("name_property");
generator.writeString(data.getName() + " XXX");
generator.writeEndObject();
}
}
Finally, you have to create two ObjectMapper
's. Simple usage:
Data data = new Data();
data.setName("Tom");
Root root = new Root();
root.setData(data);
ObjectMapper mapperWithMixIn = new ObjectMapper();
mapperWithMixIn.addMixInAnnotations(Root.class, RootMixIn.class);
ObjectMapper mapperDefault = new ObjectMapper();
System.out.println("With MIX-IN");
System.out.println(mapperWithMixIn.writeValueAsString(root));
System.out.println("Default");
System.out.println(mapperDefault.writeValueAsString(root));
Above script prints:
With MIX-IN
{"data":{"name_property":"Tom XXX"}}
Default
{"data":{"name":"Tom"}}
As you can see, ObjectMapper
with MixIn
you can use in request handlers, and default ObjectMapper
you can use in RestTemplate.
Update 1
Spring
creates default ObjectMapper
bean and uses it to serialize and deserialize request's data. You have to find and override this default bean. How to do it? Please, see below links:
This overridden bean should look like mapperWithMixIn
in my above example.
Secondly, you have to create new ObjectMapper
bean and inject this bean to all RestTemplate-s and use this bean in these classes as a serializer/deserializer.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…