SpringBoot中对于RestTemplate的基本封装

本文介绍了如何在SpringBoot项目中配置RestTemplate,设置超时时间,并封装GET和POST请求的方法。通过@Autowired注解注入RestTemplate,展示了如何发送带有头部信息的HTTP请求并处理响应结果。
摘要由CSDN通过智能技术生成
  • 核心配置,注入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);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JAVA-葵花宝典

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值