【发布时间】:2017-08-11 16:56:30
【问题描述】:
我想创建一个 REST API 客户端,它对于同一个 REST API 服务器将调用不同的 URL。这种不同的调用将向客户端返回 JSON 和 XML。 什么是适合这种情况的好的设计模式?
到目前为止,我已经想出了策略和命令的组合:
public interface IRestCall {
/** Setup the URL for the call. */
void setup(IRestSetup setup) throws Exception;
/** Execute the call, using the URL set up in the first step.
* @throws Exception */
void call() throws Exception;
/** Process the result and return to the user. */
<T> T getResult(IRestResultProcessor<T> result) throws Exception;
}
这是策略界面。该战略的背景将 在 Facade 类的某些 Get/Post/Put 方法中。
IRestSetup 和 IRestResultProcessor 是 Command 对象的接口 这将为 REST API 设置 URL 并处理结果。
【问题讨论】:
-
在服务器端解析 JSON 的好设计是使用 GSON。 journaldev.com/2321/gson-example-tutorial-parse-json 您必须设计适当的 JavaBean 来解析和验证 JSON 响应。对于 XML,您可以使用 JAXB 绑定。
-
感谢您的回答,但这只是我问题的一小部分。另外,我对设计感兴趣,而不是特定的框架。
标签: java rest design-patterns