我有一个REST服务和https://api.myrestservice.com
之类的外部服务器,并且我有一个在http://localhost:8080
上本地运行的Spring Boot应用程序。现在,我想使用本地运行的Spring Boot应用程序(即通过{{)向REST API地址(即https://api.myrestservice.com/users
发出 GET 或 POST 请求以获取所有用户) 1}}。我不知道如何将本地应用程序请求重定向到外部服务器请求。
答案 0 :(得分:1)
希望您的问题正确。您正在尝试获取本地应用,以从服务器上运行的应用获取数据。
您可以在Spring Boot应用程序中使用以下示例代码。
private void getUsers() { final String uri = "https://api.myrestservice.com/users"; RestTemplate restTemplate = new RestTemplate(); Users result = restTemplate.getForObject(uri, Users.class); System.out.println(result); }
然后,您的getUsers可以由您的spring boot应用程序中的getUsers Controller调用。
如果您想查看更多示例,请添加参考- https://howtodoinjava.com/spring-restful/spring-restful-client-resttemplate-example/
答案 1 :(得分:1)
将Api调用从您的代码发布到另一台服务器:
假设您有一台服务器https://searchEmployee ...,该服务器将向您返回属于特定城市或特定组织的员工列表:
请求正文:
{
"city" : "Ranchi",
"organisation" : "Bank Of America"
}
json响应:[{"name": "Vikash"},{"name":"kumar" },{}...etc]
然后要进行api调用,您可以像这样在Java中使用RestTemplate:
public void makeApiCall(){
final String uri = "https://searchEmployee...";
RestTemplate restTemplate = new RestTemplate();
String reqBody = "{"city": "Ranchi"}";
String result = restTemplate.postForObject(uri, reqBody, String.class);
// convert your result into json
try {
jsonResponse = new JSONObject(result);
} catch (JSONException e) {
e.printStackTrace();
}
//extract a value "name" from your json data:
try{
String value = jsonResponse.getString("name");
}catch(JSONException e) {
e.printStackTrace();
}
}
/ ************************************************** ********************** /
如果要设置多个请求正文参数,请按以下步骤操作:
String reqBody = "{\"quantity\":100,\"name\":\"product1\",\"ifBoolean\":false}";
false是请求正文中的布尔值,而100是整数。
注意 如果您在设置请求正文时遇到问题,请直接从邮递员请求正文中复制并粘贴到双引号中。
答案 2 :(得分:0)
有很多方法可以做到这一点。像Apache HTTP组件等。样本
String type = "application/x-www-form-urlencoded" Or Set your desire content type;
String encodedData = URLEncoder.encode( rawData, "UTF-8" );
URL u = new URL("your remote url");
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty( "Content-Type", type );
conn.setRequestProperty( "Content-Length",
String.valueOf(encodedData.length()));
OutputStream os = conn.getOutputStream();
os.write(encodedData.getBytes());
这里发生了两件事,就像URLEncoding在安全性方面确实很重要。 注意:以上代码的来源:here。
答案 3 :(得分:0)
这非常简单通过使用Java客户端,您可以使用RestTemplate或UniRest 在Remote上运行的只是一个Producer,而在本地运行的是Consumer,因此您可以交换Resttemplate的方法或获取Unirest的方法 示例代码在这里。
@RequestMapping(value = "/testclient")
public String testclient()
{
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>(headers);
return restTemplate.exchange("https://www.mocky.io/v2/5185415ba171ea3a00704eed", HttpMethod.GET, entity, String.class).getBody();
}
对于Unirest代码来说就是这样
HttpResponse<JsonNode> jsonResponse = null;
try {
jsonResponse = Unirest.get("https://www.mocky.io/v2/5185415ba171ea3a00704eed")
.header("accept", "application/json").queryString("apiKey", "123").asJson();
} catch (UnirestException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
return jsonResponse.getBody().toString();