• Spring的RestTemplate功能举例


    import com.google.common.collect.Maps;
    import com.shein.dms.common.BasicCase;
    import lombok.Data;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.*;
    import org.springframework.web.client.RestTemplate;
    import org.testng.annotations.Test;
    
    import java.util.HashMap;
    
    /**
     * @author :gongxr
     * @description:测试RestTemplate 参考文档:https://github.com/itguang/springcloud-learn/tree/master/resttemplate-test
     */
    @Slf4j
    public class TestRestTemplate extends BasicCase {
        public String urlPath = "http://www.baidu.com";
    
        @Autowired
        RestTemplate restTemplate;
    
        UserEntity userEntity;
    
        @Test
        public void testGet() {
            ResponseEntity<String> responseEntity = restTemplate.getForEntity(urlPath, String.class);
            log.info(responseEntity.getBody());
    
            // 有参数的GET方法
            HashMap<String, String> map = new HashMap<>();
            map.put("id", "aaa");
            ResponseEntity<UserEntity> responseEntity2 = restTemplate.getForEntity("http://localhost/get/id={id}", UserEntity.class, map);
            UserEntity userEntity = responseEntity2.getBody();
        }
    
        @Test
        public void testGet2() throws Exception {
            String url = sysConfig.getDmsUrl() + "/dms/presets/logs?id=4486";
            // 消息头
            HttpHeaders headers = new HttpHeaders();
            headers.add("cookie", commonService.getDmsCookie2("关健"));
    //        消息体
            HashMap<String, Object> bodyMap = Maps.newHashMap();
            HttpEntity httpEntity = new HttpEntity(bodyMap, headers);
    //        发送请求
            ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
            log.info("请求地址:{}", url);
            log.info("消息头:{}", headers.toString());
            log.info("请求消息体:{}", httpEntity.getBody().toString());
            log.info("响应消息:{}", responseEntity.getBody());
        }
    
        @Test
        public void testPostDemo() throws Exception {
            String url = sysConfig.getDmsUrl() + "/dms/vmiAutoOrderSupplier/list";
            // 消息头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            headers.add("cookie", commonService.getDmsCookie2("关健"));
    //        消息体
            HashMap<String, Object> bodyMap = Maps.newHashMap();
            bodyMap.put("title", "");
            bodyMap.put("pageNumber", 1);
            bodyMap.put("pageSize", 3);
            HttpEntity httpEntity = new HttpEntity(bodyMap, headers);
    //        发送请求
            ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
            log.info("请求地址:{}", url);
            log.info("消息头:{}", headers.toString());
            log.info("请求消息体:{}", httpEntity.getBody().toString());
            log.info("响应消息:{}", responseEntity.getBody());
        }
    
        @Data
        protected class UserEntity {
            private String name;
            private int age;
        }
    
    }
  • 相关阅读:
    【bzoj题解】2186 莎拉公主的困惑
    【算法学习】整体二分
    【算法学习】【洛谷】cdq分治 & P3810 三维偏序
    【比赛游记】NOIP2017游记
    【0】如何在电脑中使用多个python版本【python虚拟环境配置】
    Mysql 安装服务无法启动解决方案与使用的一般使用指令
    4-urllib库添加代理,添加请求头格式 模板
    3-urllib的post请求方式
    02-urllib库的get请求方式
    01-urllib库添加headers的一般方法
  • 原文地址:https://www.cnblogs.com/gongxr/p/16360970.html
Copyright © 2020-2023  润新知