首页 > 编程语言 > 详细

SpringBoot中RestTemplate基本封装

时间:2020-11-29 14:40:41      阅读:504      评论:0      收藏:0      [点我收藏+]
核心配置,注入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);
    }

 

SpringBoot中RestTemplate基本封装

原文:https://www.cnblogs.com/coderdxj/p/14056089.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!