• SpringBoot中RestTemplate基本封装


    核心配置,注入RestTemplate为Bean
    package com.example.demo.config;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.client.SimpleClientHttpRequestFactory;
    import org.springframework.web.client.RestTemplate;
    @Configuration
    public class HttpConfig {
        /**
         * 两分钟超时时间
         */
        private int outtime = 2 * 60 * 1000;
        @Bean
        public RestTemplate restTemplate() {
            SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
            requestFactory.setConnectTimeout(outtime);
            requestFactory.setReadTimeout(outtime);
            return new RestTemplate();
        }
    }
    

      

    封装GET/POST请求
    package com.example.demo.service;
    import com.alibaba.fastjson.JSONObject;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.HttpEntity;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpMethod;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Service;
    import org.springframework.web.client.RestTemplate;
    import java.util.Map;
    /**
     * Http请求Service
     */
    @Service
    public class HttpService {
        @Autowired
        private RestTemplate restTemplate;
        /**
         * get请求
         *
         * @param url       请求地址
         * @param headerMap 头部信息
         * @param resp      响应结果类型
         * @return
         */
        public Object get(String url, Map<String, String> headerMap, Class<?> resp) {
            HttpHeaders httpHeaders = new HttpHeaders();
            for (Map.Entry<String, String> stringStringEntry : headerMap.entrySet()) {
                httpHeaders.add(stringStringEntry.getKey(), stringStringEntry.getValue());
            }
            HttpEntity httpEntity = new HttpEntity(httpHeaders);
            ResponseEntity<?> result = restTemplate.exchange(url, HttpMethod.GET, httpEntity, resp);
            return result.getBody();
        }
        /**
         * post 请求
         *
         * @param url        请求地址
         * @param headerMap  头信息
         * @param jsonObject 请求参数 JSON
         * @return JSONObject
         */
        public JSONObject post(String url, Map<String, String> headerMap, JSONObject jsonObject) {
            HttpHeaders httpHeaders = new HttpHeaders();
            for (Map.Entry<String, String> stringStringEntry : headerMap.entrySet()) {
                httpHeaders.add(stringStringEntry.getKey(), stringStringEntry.getValue());
            }
            HttpEntity httpEntity = new HttpEntity(jsonObject, httpHeaders);
            JSONObject result = restTemplate.postForObject(url, httpEntity, JSONObject.class);
            return result;
        }
    }
    

      

    测试HttpService
    /**
         * 测试get
         */
        @Test
        public void getService() {
            Map<String, String> map = new HashMap<>();
            map.put("abc", "23434");
            String obj = (String) httpService.get("http://127.0.0.1:8080/b", map, String.class);
            System.out.println(obj);
        }
        /**
         * 测试post
         */
        @Test
        public void postService() {
            Map<String, String> map = new HashMap<>();
            map.put("abc", "54545");
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name", "孙悟空");
            JSONObject obj = httpService.post("http://127.0.0.1:8080/a", map, jsonObject);
            System.out.println(obj);
        }
  • 相关阅读:
    多线程:多线程设计模式(一):总体介绍
    javascript:12种JavaScript MVC框架之比较
    mysql 查询死锁语句
    charles 抓包工具破解方法
    java 自定义log类
    git统计日期之间的代码改动行数
    mac/linux自带定时任务执行crontab的使用
    python MD5步骤
    python 操作excel读写
    python logger日志工具类
  • 原文地址:https://www.cnblogs.com/coderdxj/p/14056089.html
Copyright © 2020-2023  润新知