mardi 16 janvier 2018

What design-pattern should I apply to manage parameters in a REST API

I have a REST API with JSON communication. I would like to have only one link accepts a json string POST with all needed parameters. For this purpose,

I first created a wrapper class like the following to make the incoming json string to an object

public class ReqWrapper{
    String action;
    Object parameters;
    String actionBy;
}

"parameters" object's type is Object, because every action has a related class. For example, I have a site registration module and the system keep many web sites. To get them, here is the getter parameters :

public class GetSite {
    String slotId;
    String userId;
    String siteId;
    String statusId;
    String adminId;
    String categoryId;
    String page;
    String size;
    String searchQuery;
    String []statusIds;
    String lang;
}

The above class has some potential parameters that may be used to get web sites, but I have doubts that it is the best approach.

The final JSON request is expected to be the following from the client :

{
  "action": "get_site",
  "parameters": {
   "userId": "1",
   "statusId": "11",
   "actionBy": "Admin"
  }
}

To get the parameters, first it is converted to ReqWrapper then its "parameters" object is casted to GetSite object.

I don't really like this solution and wondering a design pattern to manage parameters in such systems. Is there a better way rather than keeping the parameters in different classes according to their request type and casting them to proper objects ? Creating the potential variables in one class is a proper approach ?

I have done some research in web & stackoverflow and found a question close to this one here . However, what I want is not solving the difference of Response and Request classes.

Aucun commentaire:

Enregistrer un commentaire