zoukankan      html  css  js  c++  java
  • 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;
    }
    

     

     

  • 相关阅读:
    shell 指令
    在Linux下搭建nRF51822的开发烧写环境(makefile版)
    宏定义。字符串拼接和字符串整形转字符串
    django-debug-toolbar安装过程中的error
    pipenv
    Docker 命令大全
    MySQL性能优化
    docker操作
    使用网易源解决docker下载镜像文件慢的问题
    w3school/jQuery 教程
  • 原文地址:https://www.cnblogs.com/cnsec/p/13407144.html
Copyright © 2011-2022 走看看