简体   繁体   English

添加字段以响应 API 时的向后兼容性

[英]Backward Compatibility when adding a field in response of an API

I have added a couple of new fields to a Json response of an object .It rus fine when we update the client to hit the API .我在对象的 Json 响应中添加了几个新字段。当我们更新客户端以命中 API 时,它很好。 But older versions of the API give a serialization error.但是旧版本的 API 会出现序列化错误。 I am using PROTOSTUFF to serialize and deserialize the object .我正在使用PROTOSTUFF来序列化和反序列化对象。 How can I make the old version of client to ignore the new fields and continue to work properly .如何让旧版本的客户端忽略新字段并继续正常工作。 I have tried making the fields transient and @JsonIgnore Tag我曾尝试使字段瞬态和 @JsonIgnore标记

The solution was to add the extra fields at the end of all fields in the class instead of in the middle .解决方案是在类中所有字段的末尾而不是中间添加额外的字段。 PROTOSTUFF ignores the fields added at the end as unknown fields and doesn't throw an error . PROTOSTUFF 忽略最后添加的字段作为未知字段并且不会抛出错误。

You need version control in your controller.您需要在控制器中进行版本控制。 You must implement a versioning mechanism to figure out which method should be called in witch version... it's near impossible to handle this issue with a single action in the controller.您必须实现一个版本控制机制来确定应该在女巫版本中调用哪个方法......在控制器中使用单个操作来处​​理这个问题几乎是不可能的。

See below link:见以下链接:

REST API Versioning REST API 版本控制

Introduction to API Versioning Best Practices API 版本控制最佳实践简介

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

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