简体   繁体   English

通过具有多个输入的Java中的REST API发送GET请求

[英]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.

相关问题 Java REST API 发送 GET 请求时出错,但在 POST 上工作 - Java REST API Getting Error when sending GET Request, but works on POST 如何使用java在REST api中通过post请求将多个json文档发送到marklogic数据库? - How to send multiple json documents into marklogic database through post request in REST api using java? 使用Java从Playframework中的控制器向REST API发送发布请求 - Sending a post request to REST API from controller in Playframework with Java 在 Postman 的请求正文中传递多个 JSON 数据并使用 Jersy(JXRS) 进入 Java Rest API - Pass multiple JSON data in Request Body of Postman and Get into Java Rest API using Jersy(JXRS) 发送请求有效载荷到Rest API Java修改代码,而无需使用Java中的流读写器 - Sending request payload to rest api java modify the code without stream reader and writer in java 通过java中的Rally Rest API查询获取项目的ObjectID - Querying to get the ObjectID of the Project through Rally Rest API in java 从 Java 获取 GET 请求参数到 Python Hug REST API - Fetching GET request parameters from Java to Python Hug REST API Java REST api PATCH 请求 - Java REST api PATCH request Sending POST request with POSTMAN to Java REST API and getting null values in the xml response - Sending POST request with POSTMAN to Java REST API and getting null values in the xml response Jersey Api 未通过过滤器发送请求 - Jersey Api not sending request through Filter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM