如何将 HashMap 作为参数传递

huangapple go评论96阅读模式
英文:

How can I pass a HashMap as parameters

问题

我有一个名为 API 的类,在这个类中,你可以设置和获取稍后在 API 调用中需要使用的信息。我希望能够让下一个使用这个类的人更加方便。

所以,不是像这样做:

api.addURL("urltorequesttoken");
api.addHeader("client_id","sdfsfsdfsd")
   .addHeader("client_secret","sdfsdfsfsfd")
   .addHeader("grant_type","client_credentials")
   .addHeader("scope","READ");
api.addBody("bodyToSend")

我想这样做:

String URL = "";
URL = "put your URL here";

所以,我将 URL 和其他变量作为参数传递给另一个方法,在这个方法中,我将执行我在第一个代码块中所做的操作,这样他们就不需要了解 API 类及其方法,但是我不知道如何处理 HashMap,如何以用户友好的方式实现?然后将其作为参数传递,另外,接收这些信息的方法应该具有什么类型的参数?(Map<String, String>)还是(String key, String value)?

编辑(添加):

所以有一个 DEV 将要创建的类,让我们称之为 CreateToken,那个类当前有:

api.addURL("urltorequesttoken");
    api.addHeader("client_id","sdfsfsdfsd")
       .addHeader("client_secret","sdfsdfsfsfd")
       .addHeader("grant_type","client_credentials")
       .addHeader("scope","READ");
    api.addBody("bodyToSend")

还有另一个名为 BASE 的类,在这个类中,我正在执行核心服务,为了让他们在创建自己的类时更容易,我不想在他们的类中放置那个代码块,而是放在我的类中,所以在他们的类中,他们只需设置 URL、headers 和 body(对于 POST 方法),所以不是这样做:

api.addURL("urltorequesttoken");

他们将会这样做:

URL = "urltorequesttoken";

在他们的类中有一个方法将这些信息发送给我,或者让我获取它,例如:

fillAPICallInfo(URL, headers, body);

我会在 BASE 类中接收到这些信息,但是我不知道如何处理 Map 变量,不知道如何使得 DEV 只需放入键和值,以及我如何在我的类中接收这些信息(作为 Map 还是作为字符串)?

英文:

I have a class API, where you set and get the info you need to later be used on a API call
I want to make it easier to the next person who's gonna use this.

So instead of doing this:

api.addURL(&quot;urltorequesttoken&quot;);
api.addHeader(&quot;client_id&quot;,&quot;sdfsfsdfsd&quot;)
   .addHeader(&quot;client_secret&quot;,&quot;sdfsdfsfsfd&quot;)
   .addHeader(&quot;grant_type&quot;,&quot;client_credentials&quot;)
   .addHeader(&quot;scope&quot;,&quot;READ&quot;);
api.addBody(&quot;bodyToSend&quot;)

I want to do this:

String URL = &quot;&quot;;
URL = &quot;put your URL here&quot;;

So I pass the URL and other variables as a parameter to another method where I will be doing what I did in the first block of code,so they don't need to know about the API class and its methods, but I dont know how to handle the hashmap, how can I do that user friendly? and then pass that as a parameter, also, what type of parameters should the methods receiving this info have? (Map<String key, String value>) or (String key, String value)?

EDIT(ADD):

So there's a class that a DEV is going to create, let's call it CreateToken
, so that class currently has:

api.addURL(&quot;urltorequesttoken&quot;);
api.addHeader(&quot;client_id&quot;,&quot;sdfsfsdfsd&quot;)
   .addHeader(&quot;client_secret&quot;,&quot;sdfsdfsfsfd&quot;)
   .addHeader(&quot;grant_type&quot;,&quot;client_credentials&quot;)
   .addHeader(&quot;scope&quot;,&quot;READ&quot;);
api.addBody(&quot;bodyToSend&quot;)

There's another class called BASE, where Im doing the core services, in order for this to be easier for the person when they create their class, I dont want to have that block of code on their class, but instead, on mine, so in their class all they have to do is set the URL, headers and body(for POST method), so instead of this:

api.addURL(&quot;urltorequesttoken&quot;);

they will do:

URL = &quot;urltorequesttoken&quot;;

and there's a method on their class to send me this or for me to get it i,e.

fillAPICallInfo(URL, headers, body);

I will receive that on the BASE class, but I dont know how to handle the Map variables, don't know how to make it easy for the DEV so they just put the key and value, and how do I receive that on my class (as a Map or as Strings)?

答案1

得分: 1

好的,以下是您要求的翻译内容:

所以您只需将一个 Map<String, String> 作为参数传递:

public void fillAPICallInfo(String url, Map<String, String> headers, String body) {
  // 假设有一个名为 api 的类 DEV 的实例可用
  api.addURL(url);
  headers.forEach((h, v) -> api.addHeader(h, v));
  api.addBody(body);
}
英文:

So you simply can pass a Map&lt;String, String&gt; as parameter:

public void fillAPICallInfo(String url, Map&lt;String, String&gt; headers, String body) {
  // Assuming there is an instance of class DEV named api available
  api.addURL(url);
  headers.forEach((h, v) -&gt; api.addHeader(h, v));
  api.addBody(body);
}

huangapple
  • 本文由 发表于 2020年7月24日 06:03:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63063774.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定