博主
258
258
258
258
专辑

第四节 第三方接口访问:使用RestTemplate发送HTTP请求

亮子 2023-05-17 04:11:01 3117 0 0 0

1、添加依赖

需要在SpringBoot环境下使用

2、演示代码

package com.shenma2009;

import lombok.Data;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 军哥
 * @version 1.0
 * @description: TestRestTemplateApp
 * @date 2023/5/17 11:19
 */

@Data
class CommParamVo {
    private Integer userId;
    private String message;
}

public class TestRestTemplateApp {

    /**
     * @description POST方法传递JSON字符串
     * @author 军哥
     * @date 2023/5/17 11:23
     * @version 1.0
     */
    @Test
    public void test1(){
        try{
            RestTemplate restTemplate = new RestTemplate();
            //https://shenmazong.com/test/sendEmail
            String url = "https://shenmazong.com/test/sendEmail";
            CommParamVo commParamVo = new CommParamVo();
            commParamVo.setUserId(1001);
            commParamVo.setMessage("hello");
            String s = restTemplate.postForObject(url, commParamVo, String.class);
            System.out.println(s);
            return;
        }
        catch(Exception exp){
            System.out.println(exp);
            return;
        }
    }

    /**
     * @description GET方法,通过路径传参
     * @author 军哥
     * @date 2023/5/17 11:24
     * @version 1.0
     */
    @Test
    public void test2(){
        try{
            RestTemplate restTemplate = new RestTemplate();
            //https://shenmazong.com/test/sendEmail
            String url = "https://shenmazong.com/test/sendSms/1001/hello";
            String s = restTemplate.getForObject(url, String.class);
            System.out.println(s);
            return;
        }
        catch(Exception exp){
            System.out.println(exp);
            return;
        }
    }

    /**
     * @description POST方法传递form表单
     * @author 军哥
     * @date 2023/5/17 11:28
     * @version 1.0
     */
    @Test
    public void test3(){
        try{
            //https://shenmazong.com/test/sendEmail
            RestTemplate restTemplate = new RestTemplate();
            String url = "https://shenmazong.com/test/login";
            LinkedMultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
            multiValueMap.add("username","杰尼瑞");
            multiValueMap.add("userpass","123456");
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity<LinkedMultiValueMap<String, Object>> request = new HttpEntity<>(multiValueMap, httpHeaders);
            ResponseEntity<String> resp = restTemplate.postForEntity(url, request, String.class);
            String body = resp.getBody();
            System.out.println(body);
            return;
        }
        catch(Exception exp){
            System.out.println(exp);
            return;
        }
    }

    /**
     * @description GET方法无参
     * @author 军哥
     * @date 2023/5/17 11:31
     * @version 1.0
     */

    @Test
    public void test4(){
        try{
            //https://shenmazong.com/test/sendEmail
            RestTemplate restTemplate = new RestTemplate();
            String url = "https://shenmazong.com/test/getRandCode";
            String s = restTemplate.getForObject(url, String.class);
            System.out.println(s);
            return;
        }
        catch(Exception exp){
            System.out.println(exp);
            return;
        }
    }

    /**
     * @description TODO
     * @author 军哥
     * @date 2023/5/17 12:01
     * @version 1.0
     */
    @Test
    public void testE(){
        try {
            String url = "https://shenmazong.com/test/getMd5?message=";
            RestTemplate restTemplate = new RestTemplate();
            String message = URLEncoder.encode("123456", "UTF-8");
            String res = restTemplate.getForObject(url+message, String.class);
            System.out.println(res);
            return;
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

    }

}