简体   繁体   中英

how to generalize apachi http client post parameters

I am trying to make an open-source generic payment gateways library on java. where we will integrate all types of payment gateways so other developers can easily integrate any payment gateway in there system.

But i stuck on a point as you know data could be posted in two ways.

  • old method para1=val1&para2=val2
  • new one (JSON) {para1:val1, para2:val2}

For http post i choose apache http client but HttpPost accept List

List<NameValuePair> postParams = new ArrayList<>();
postParams.add(new BasicNameValuePair("name", "test"));
postParams.add(new BasicNameValuePair("email", "testemail@domain.com"));
httpPost.setEntity(new UrlEncodedFormEntity(postParams))

for json i am using Gson google json library for java.

I would like to make a parameter layer that could be easily converted in both of the form explained above.

I would like to take care of complex parameters too. Like, card[name]=some name & card[no] = 14444444444

your expert advise is highly appreciable.

This answer was written based on the code being the server

Translation layer

Translator translates the request into an internal representation of an action, then your Data Controller acts on this action object.

You could restructure your request at a translation layer, so that the Data Controller does not care about how the request is made, only the data contained.

This could also be used for returning data, so XML and JSON output can be created with the same Data Controller .

在此处输入图片说明

Edit

This should be reversed, so actions are translated to requests, in the client case, which turns out to be the correct one. This reverses all arrows on the diagram

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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