zoukankan      html  css  js  c++  java
  • 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);
        }
  • 相关阅读:
    NOI2005 聪聪和可可
    CodeVS 1344 线型网络
    BZOJ 2466: [中山市选2009]树
    BZOJ 3827: [Poi2014]Around the world
    BZOJ 1109: [POI2007]堆积木Klo
    BZOJ 2124: 等差子序列
    BZOJ 4544: 椭圆上的整点
    BZOJ 2342: [Shoi2011]双倍回文
    BZOJ 2084: [Poi2010]Antisymmetry
    BZOJ 3111: [Zjoi2013]蚂蚁寻路
  • 原文地址:https://www.cnblogs.com/coderdxj/p/14056089.html
Copyright © 2011-2022 走看看