繁体   English   中英

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

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

我即将针对REST API开发Java SDK,并想知道构建它的最佳实践方法是什么。 我看过谷歌,并且还使用了许多连接到REST API的SDK,而且没有太多的一致性。 我遇到过一些我感兴趣并且想知道哪种模式可以被认为是最佳实践的模式,如果有的话,或者是否有替代方案?

我提供了样本/伪代码以方便。

1)模型/请求/客户端都是分开的。 示例电话:

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)模型和请求捆绑在一起,客户端是分开的。 示例电话:

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)模型包含一切。 示例电话:

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... }

如果有更好的方法来建立这个,我也很高兴听到他们。

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

暂无
暂无

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

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