Spring RestTemplate 发送GET和POST请求 适用于80%的场景

Spring RestTemplate 使用:

发送GET请求:

示例1:发送最简单的GET请求

public String sendGetRequest() {

    ResponseEntity<String> responseEntity = restTemplate

            .exchange("https://www.test.com/testAPI", HttpMethod.GET, null, String.class);

    return responseEntity.getBody();

}

 

示例2:发送带头部信息的GET请求:

public String sendGetRequest() {

    DEBUG.debug("======Send Request [Start]========");
    String url = "https://www.test.com/testAPI";
    HttpHeaders headers = new HttpHeaders();
    headers.set("OtherHeadersxxx", "xxxx"); //Other headers
    HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity(headers);

    DEBUG.debug("Request URL: " + url);
    DEBUG.debug("Request Method: " + HttpMethod.GET);
    DEBUG.debug("Request Headers: " + httpEntity.getHeaders().toString());

    ResponseEntity<String> responseEntity = restTemplate
            .exchange(url, HttpMethod.GET, httpEntity, String.class);
    String responseBody = responseEntity.getBody();
    DEBUG.debug("Response Body: " + responseBody);
    DEBUG.debug("======Send Request[End]======");
    return responseBody;

}

 

 

发送POST请求:

可以设置一些头信息,比如Content-Type, Authentication….等等

示例1:直接填写request body的,比较适用于JSON或者XML格式的请求体

public String sendPostRequest() {
    DEBUG.debug("======Send Request [Start]========");
    String requestBody = "{\"id\": \"test111\"}";
    String url = "https://www.test.com/testAPI";
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);//Content-Type
    headers.set("OtherHeadersxxx", "xxxx"); //Other headers
    HttpEntity<String> httpEntity = new HttpEntity<>(requestBody, headers);

    DEBUG.debug("Request URL: " + url);
    DEBUG.debug("Request Method: " + HttpMethod.POST);
    DEBUG.debug("Request Headers: " + httpEntity.getHeaders().toString());
    DEBUG.debug("Request Body: " + httpEntity.getBody());

    ResponseEntity<String> responseEntity = restTemplate
            .exchange(url, HttpMethod.POST, httpEntity, String.class);
    String responseBody = responseEntity.getBody();
    DEBUG.debug("Response Body: " + responseBody);
    DEBUG.debug("=======Send Request[End]=========");
    return responseBody;
}

示例2:表单式的POST请求,一个个加参数

public String sendPostRequest() {
    DEBUG.debug("======Send Request [Start]========");
    String url = "https://www.test.com/testAPI";
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);//Content-Type
    headers.set("OtherHeadersxxx", "xxxx"); //Other headers
    MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
    params.add("param1", "testxxx");
    params.add("param2","testxxx");
    params.add("param3", "testxxx");
    HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity(params, headers);

    DEBUG.debug("Request URL: " + url);
    DEBUG.debug("Request Method: " + HttpMethod.POST);
    DEBUG.debug("Request Headers: " + httpEntity.getHeaders().toString());
    DEBUG.debug("Request Body: " + httpEntity.getBody());

    ResponseEntity<String> responseEntity = restTemplate
            .exchange(url, HttpMethod.POST, httpEntity, String.class);
    String responseBody = responseEntity.getBody();
    DEBUG.debug("Response Body: " + responseBody);
    DEBUG.debug("======Send Request[End]======");
    return responseBody;
}

 

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值