为RESTAPI构建JavaSDK的最佳实践

为RESTAPI构建JavaSDK的最佳实践,java,rest,sdk,Java,Rest,Sdk,我将针对RESTAPI开发一个JavaSDK,并想知道构建它的最佳实践方法是什么。我研究过谷歌,也使用了许多连接到RESTAPI的SDK,但从来没有多少一致性。我遇到了一些我觉得有趣的模式,我想知道哪一种可以被认为是最佳实践,如果有的话,或者是否有其他的选择 我已经提供了示例/伪代码来帮助您 1) 模型/请求/客户机都是分开的。示例调用: Client client = new Client( ... credentials ... ); try { Something obj = c

我将针对RESTAPI开发一个JavaSDK,并想知道构建它的最佳实践方法是什么。我研究过谷歌,也使用了许多连接到RESTAPI的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... }
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... }
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... }
2) 模型和请求绑定在一起,客户机是独立的。示例调用:

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... }
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... }
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... }
3) 模型包含所有内容。示例调用:

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... }
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... }
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服务调用的方法名,并且不会太通用。

我同意。对于一个复杂的RESTAPI,其设计可能会有所不同,例如,AWS SDK并不像这一个那么简单,而是以一种非常不同的方式构造的。然而,我们非常直截了当,只在一系列资源上提供CRUD操作,而不是为REST服务构建JavaAPI,而是一个通用的REST客户端库。