Java 使用Spring Boot获取/发布REST API的请求
我有一个REST服务,一个像Java 使用Spring Boot获取/发布REST API的请求,java,rest,spring-boot,request-mapping,Java,Rest,Spring Boot,Request Mapping,我有一个REST服务,一个像https://api.myrestservice.com我有一个Spring Boot应用程序在http://localhost:8080。现在我想对restapi地址发出GET或POST请求,即https://api.myrestservice.com/users使用我本地运行的Spring Boot应用程序,即通过http://localhost:8080/users。我不知道如何将本地应用程序请求重定向到外部服务器请求。我希望我正确地回答了你的问题。您正在尝试
https://api.myrestservice.com
我有一个Spring Boot应用程序在http://localhost:8080
。现在我想对restapi地址发出GET或POST请求,即https://api.myrestservice.com/users
使用我本地运行的Spring Boot应用程序,即通过http://localhost:8080/users
。我不知道如何将本地应用程序请求重定向到外部服务器请求。我希望我正确地回答了你的问题。您正在尝试让本地应用从服务器上运行的应用获取数据
您可以在spring boot应用程序中使用以下示例代码
然后,您的getUsers可以由spring boot应用程序中的getUsers控制器调用
如果您想查看更多示例,我将添加参考-
有很多方法可以做到这一点。像ApacheHTTP组件和其他组件。样品
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在安全性方面非常重要。
注意:以上代码的来源:。从您的代码向另一台服务器进行post Api调用: 假设你有一个服务器。。。返回属于特定城市或特定组织的员工列表: 请求机构:
{
"city" : "Ranchi",
"organisation" : "Bank Of America"
}
json响应:[{“name”:“Vikash”}、{“name”:“kumar”}、{}……等等]
然后,要进行post 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}";
/********************************************************************/
如果要设置多个请求正文参数,请执行以下操作:
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是整数
注意
如果您在设置请求正文时遇到问题,请直接从postman请求正文复制请求正文并将其粘贴到双引号中。使用Java客户端非常简单,您可以使用RestTemplate或UniRest 在远程上运行的只是生产者,在本地运行的是消费者,所以您可以交换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();
}
@RequestMapping(value=“/testclient”)
公共字符串testclient()
{
HttpHeaders=新的HttpHeaders();
setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity=新的HttpEntity(标题);
返回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();
HttpResponse jsonResponse=null;
试一试{
jsonResponse=Unirest.get(“https://www.mocky.io/v2/5185415ba171ea3a00704eed")
.header(“accept”、“application/json”).queryString(“apiKey”、“123”).asJson();
}catch(unireste异常){//TODO自动生成的catch块
e、 printStackTrace();
}
返回jsonResponse.getBody().toString();
您需要向https://api.myrestservice.com/users
。为此,您可以使用诸如REST-Template(Spring)
之类的服务,甚至可以使用诸如Unirest
之类的外部服务。搜索例子,你会发现很多不同的服务器也没关系。唯一的问题是服务器是否正常运行