我有两个POJO,看起来像:
public ResponseA {
private HeavyObject object;
private AdditionalDataA additionalData;
public ResponseA(HeavyObject object, AdditionalDataA additionalData) {
this.object = object;
this.additionalData = additionalData;
}
}
个字符
然后我有一个方法,将这些对象发送到不同的端点,类似于:
public void sendResponses(
ObjectMapper mapper,
HeavyObject object,
AdditionalDataA addDataA,
AdditionalDataB addDataB) {
// Here object is serialized twice, which is costly!
String jsonA = mapper.writeValueAsString(new ResponseA(object, addDataA));
String jsonB = mapper.writeValueAsString(new ResponseB(object, addDataB));
sendResponseA(jsonA);
sendResponseB(jsonB);
}
型
有没有一种方法可以序列化这些响应,使HeavyObject
只序列化一次?
1条答案
按热度按时间von4xj4u1#
是的,您可以使用
@JsonRawValue
注解ResponseA
和ResponseB
中的object
字段,将其转换为String
,并在单独的步骤中序列化它。