为 Web 服务建模请求和响应对象的设计模式 [英] Design Pattern to model Request and Response Objects for Webservices

查看:68
本文介绍了为 Web 服务建模请求和响应对象的设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约 7 个 REST Web 服务要实现.其中一些网络服务具有标准(相同)响应,而另一些具有不同响应.

I have about 7 REST web services to implement. Some of these web services have a standard (identical) response, while some have different responses.

对这些 Web 服务的请求是不同的,但一些请求和一些响应具有相同的底层数据对象.

The requests for these web services are different but some requests and some of the responses have the same underlying data objects.

我不确定是否必须为每个 Web 服务构建单独的请求/响应类或重用一个标准的类.我想知道是否有一种设计模式可以为这些 Web 服务的请求对象和响应对象建模.

I am not sure if I have to build separate request/response classes for each web service or reuse a standard one. I would like to know if there is a design pattern to model the request objects and response objects for these web services.

好的,假设 Account 和 Book 是我的 Web 服务将使用的两个其他资源.

Ok, say Account and Book are two rest resources my web services will be working on.

class Account {
    String username;
    String id;
}


class Book {
    String title;
    String isbn;
}

所以我的网络服务看起来像这样:

So my web services look like this:

MYAPI/CreateAccountandBook
MYAPI/Account/Create
MYAPI/Book/Create
MYAPI/Book/Update/{isbn}
MYAPI/Account/Update/{id}
MYAPI/Account/getInfo/{id} 

等等.

现在 CreateAccountandBook 请求将在有效负载中获取帐户对象和书籍列表.此外,MYAPI/Account/getInfo/{id} 的响应对象有一个帐户对象和与该帐户关联的图书列表.但是响应对象还包括一个statusCodeDescription.

Now CreateAccountandBook request will take an account object and a list of books in the payload. Also the response object for MYAPI/Account/getInfo/{id} has an account object and a list of books associated with that account. But the response object also includes a statusCode and Description.

现在我想为这些请求和响应对象创建类以最好的方式.

Now I would like to create classes for these request and response objects in the best possible way.

好的开始.

我有两个抽象类 StandardRequestStandardResponse.

I have two abstract classes StandardRequest and StandardResponse.

所有请求类都将扩展标准请求类并相应地进行自定义.所有响应类都将扩展标准响应类并进行相应的自定义.

All requests classes will extend the Standard Request class and customize accordingly. All response classes will extend the Standard response class and customize accordingly.

但是这些请求和响应可以彼此不同,但仍然重复使用相同的实体对象.

But these requests and response can be way different from each other but still re-use the same entity objects.

例如:

createAccountandBook 请求对象如下所示:

class CreateAccountAndBookRequest {
   Account account;
   List<Book> books;
}

getInfo 网络服务的响应是:

while the response for the getInfo web service is:

class GetInfoResponse {
   Account account;
   List<Book> books;
   String statusCode;
   String description;
}

因此请求和响应类之间存在重叠.我可以为每个 Web 服务创建两个(req/res)类.但是想知道是否有更好的方法来为这些类建模.

so there is overlap across request and response classes. I can create two (req/res) classes for each web service. But would like to know if there is a better way to model these classes.

推荐答案

我也有类似的困境;我朝着通用方向前进,并且喜欢结果;从此再也没有回头.

I had a similar dilemma; I went in the generic direction and am liking the results; haven't looked back since.

如果我有一个 GetAccounts API 方法,签名可能看起来像.

If I had a GetAccounts API method the signature might look like.

public final Response<Account[]> getAccounts()

当然,同样的原则可以应用于请求.

Naturally the same principle may be applied to requests.

public final Response<Account[]> rebalanceAccounts(Request<Account[]>) { ... }

在我看来;将单个实体与请求和响应解耦会产生更简洁的域和对象图.

In my opinion; decoupling the individual entities from requests and responses yields a neater domain and object graph.

下面是这种通用响应对象可能是什么样子的示例.就我而言;我构建的服务器为所有请求提供通用响应,以增强错误处理并降低域对象和响应对象之间的耦合.

Below is an example of what such a generic response object might look like. In my case; I'd built the server to have a generic response for all requests to enhance error handling and lower coupling between domain objects and response objects.

public class Response<T> {

  private static final String R_MSG_EMPTY = "";
  private static final String R_CODE_OK = "OK";

  private final String responseCode;
  private final Date execDt;
  private final String message;

  private T response;

  /**
   * A Creates a new instance of Response
   *
   * @param code
   * @param message
   * @param execDt
   */
  public Response(final String code, final String message, final Date execDt) {

    this.execDt = execDt == null ? Calendar.getInstance().getTime() : execDt;
    this.message = message == null ? Response.R_MSG_EMPTY : message;
    this.responseCode = code == null ? Response.R_CODE_OK : code;
    this.response = null;
  }

  /**
   * @return the execDt
   */
  public Date getExecDt() {

    return this.execDt;
  }

  /**
   * @return the message
   */
  public String getMessage() {

    return this.message;
  }

  /**
   * @return the response
   */
  public T getResponse() {

    return this.response;
  }

  /**
   * @return the responseCode
   */
  public String getResponseCode() {

    return this.responseCode;
  }

  /**
   * sets the response object
   *
   * @param obj
   * @return
   */
  public Response<T> setResponse(final T obj) {

    this.response = obj;
    return this;
  }
}

这篇关于为 Web 服务建模请求和响应对象的设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆