简体   繁体   English

我可以添加一个新字段以在服务器端进行响应,而旧客户端不会因杰克逊反序列化而中断吗?

[英]Can i add a new field to response at server end and the old client doesn't break with jackson deserialization?

I have a class at server : 我在服务器上上课:

Class A{
int x;
int y;
// getters and setters
}

in old client 在老客户中

Class A{
int x;
}

in new client 在新客户中

Class A{
int x;
int y;
}

now i can't change the old client code and the client gives org.codehaus.jackson.map.exc.UnrecognizedPropertyException. 现在,我无法更改旧的客户端代码,并且客户端给出了org.codehaus.jackson.map.exc.UnrecognizedPropertyException。 i can change the server or new client code. 我可以更改服务器或新的客户端代码。

How can i make this work with jackson serializer? 我该如何使用杰克逊序列化器进行此项工作?

Different format: 不同格式:

You can use Views on the server to decide during runtime which properties to include. 您可以在服务器上使用视图来确定在运行时要包括哪些属性。 The new client could then request the same content as old client, but with a different parameter/header or via different API path that simply uses a new view with new properties. 然后,新客户端可以请求与旧客户端相同的内容,但是具有不同的参数/标头或通过不同的API路径(仅使用具有新属性的新视图)。

Same format: 格式相同:

If the old client doesn't support the new format, you cannot simply force it to support it without modifying the client. 如果旧客户端不支持新格式,则不能简单地强迫它支持而不修改客户端。 In fact, if you could do something like this, it would be quite dangerous, as it would imply existing applications could not rely on their format restrictions. 实际上,如果您可以做这样的事情,那将是非常危险的,因为这意味着现有应用程序不能依赖于其格式限制。

However, if you can modify old client, here's something you can do: 但是,如果您可以修改旧客户端,则可以执行以下操作:

  1. Add @JsonIgnoreProperties(ignoreUnknown = true) to the POJO in old client 在旧客户端的POJO中添加@JsonIgnoreProperties(ignoreUnknown = true)
  2. If you can't modify POJO, but can access ObjectMapper: objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); 如果您不能修改POJO,但是可以访问ObjectMapper: objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  3. If you can access ObjectMapper, but don't want to affect all other POJOs too, you can mix-in the annotation just for that one POJO 如果您可以访问ObjectMapper,但又不想影响所有其他POJO,则可以仅为该POJO 混合注释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM