[英]Sending a GET request through a REST API in Java with multiple inputs
I need to send a request using Java to an existing REST service using GET with multiple input parameters. 我需要使用Java将请求发送到使用多个输入参数的GET的现有REST服务。
If the initial url for the calculation I want is: https://api.restservice123.com/api/calculate
如果我想要计算的初始网址是: https://api.restservice123.com/api/calculate
: https://api.restservice123.com/api/calculate
I want to make a calc object e with the following parameters: 我想使用以下参数制作calc对象e:
calcObj e = new calcObj();
e.token="token_ABC123";
e.country="US";
e.amount = 100;
e.price = 24;
e.customer = "bob";
the url should look something like this: 网址应如下所示:
https://api.restservice123.com/api/calculate?token=token_ABC123&country=US&amount=100&price=24&customer=bob
Is there any framework that will combine the parameters from the calc object and reformat them into the url appropirate format and combine them with the api url? 是否有任何框架可以结合calc对象的参数并将其重新格式化为url适当的格式,并将其与api url结合起来?
I ended up making a method in the calc object that puts all the non null parameters into a list of strings and combines them using a Joiner from google common and connecting to the url using an HttpURLConnection. 我最终在calc对象中制作了一个方法,该方法将所有非null参数放入字符串列表中,并使用来自Google common的Joiner进行合并,并使用HttpURLConnection连接到url。 But this method looks bad as I'm hoping there's something out there that can already do all of this much more elegantly, but I couldn't find it. 但是这种方法看起来很糟糕,因为我希望那里已经有一些东西可以更优雅地完成所有这些工作,但是我找不到它。
The URIBuilder class works well for this. URIBuilder类对此非常有效。 It has a method setParameters(java.util.List) that takes a list of name value pairs and builds a URI from them. 它具有setParameters(java.util.List)方法,该方法获取名称值对的列表并从中构建一个URI。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.