简体   繁体   English

REST API设计和更改“合同”?

[英]REST API design and changing “contract”?

I'm designing RESTful API. 我正在设计RESTful API。 Result will be in form of JSON object. 结果将采用JSON对象的形式。 I don't need to send all fields from my POJO classes to the client, so I should define some kind of "contract". 我不需要将我的POJO类中的所有字段发送到客户端,因此我应该定义某种“合同”。

Let's say I have a Property class, and I define id, name and address for it. 假设我有一个Property类,我为它定义了id,name和address。 Later, I might want to change address to geoData, without influencing clients. 后来,我可能想要在不影响客户端的情况下将地址更改为geoData。

Is there some elegant way (pattern) to do this? 有没有一些优雅的方式(模式)来做到这一点?

Create versioning to your REST design. 为REST设计创建版本。 When you start to create new implementation, use new URI to define it. 当您开始创建新实现时,请使用新URI来定义它。 For example create new URI with the similar parameter, with difference in version: 例如,使用类似参数创建新URI,版本不同:

  • OLD implementation /ver1/retrieve/user ( still being maintained ) OLD实现/ ver1 / retrieve / user(仍在维护中)
  • NEW implementation /ver2/retrieve/user 新实现/ ver2 / retrieve / user

By doing this, client will still able to retrieve user with old implementation without any issue. 通过这样做,客户端仍然能够检索具有旧实现的用户而没有任何问题。 However when you decide to change the implementation again, it's better for you to freeze the old version of the API ( for example when implementing v3 API, you should notify client that the old version 1 API will no longer supported ) 但是,当您决定再次更改实现时,最好冻结旧版本的API(例如,在实现v3 API时,您应该通知客户端将不再支持旧版本1 API)

More information can be found here : 更多信息可以在这里找到 :

How to version REST URIs 如何版本REST URI

Versioning restful services 版本化的宁静服务

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

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