简体   繁体   English

为REST API构建Java SDK的最佳实践

[英]Best Practice to Build a Java SDK for a REST API

I'm about to develop a Java SDK against a REST API and would like to know what would be the best practice approach to building it. 我即将针对REST API开发Java SDK,并想知道构建它的最佳实践方法是什么。 I've looked at Google and also used a number of SDKs which connect to REST APIs and there is never much consistency. 我看过谷歌,并且还使用了许多连接到REST API的SDK,而且没有太多的一致性。 I've come across some patterns which I find interesting and would like to know which one could be considered best practice, if any, or if there are alternatives? 我遇到过一些我感兴趣并且想知道哪种模式可以被认为是最佳实践的模式,如果有的话,或者是否有替代方案?

I've provided sample / pseudo code to facilitate. 我提供了样本/伪代码以方便。

1) The models / requests / client are all separated. 1)模型/请求/客户端都是分开的。 Example call: 示例电话:

Client client = new Client( ... credentials ... );

try {
    Something obj = client.post( new PostSomethingRequest( ..params... ) );
} catch( Exception oops ) { ...handle... }

try {
    Something obj2 = client.get( new GetSomethingRequest( id ) );
} catch( Exception oops ) { ...handle... }

2) The models and request are tied together and the client is separate. 2)模型和请求捆绑在一起,客户端是分开的。 Example call: 示例电话:

Client client = new Client( ... credentials ... );

try {
    Something obj = client.post( new Something( ..params... ) );
} catch( Exception oops ) { ...handle... }

try {
    Something obj2 = client.get( new Something( id ) );
} catch( Exception oops ) { ...handle... }

3) The model contains everything. 3)模型包含一切。 Example call: 示例电话:

Client.setCredentials( ... credentials ... );

Something obj = new Something( ..params... );
try {
    obj.post();
} catch( Exception oops ) { ...handle... }

try {
    Something obj2 = Something.get( id );
} catch( Exception oops ) { ...handle... }

If there are better ways of building this I'd also be glad to hear about them. 如果有更好的方法来建立这个,我也很高兴听到他们。

如果你为一个特殊的REST api构建一个sdk,我会使用代表REST服务调用的方法名称,并且不会如此通用。

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

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