本文为博主原创,未经允许不得转载:

  1.对常用调用的方法进行封装:

import org.springframework.http.HttpHeaders;

import com.alibaba.fastjson.JSONObject;

public interface SpringRestService {

    public <T> T postWithJson(String url, JSONObject param, Class<T> responseType);

    public <T> T postWithJson(String url, HttpHeaders headers, JSONObject param, Class<T> responseType);

    public <T> T postWithJson(String url, HttpHeaders headers, String param, Class<T> responseType);

    public <T> T getForObject(String url, Class<T> responseType);

    public String getForObject(String url, HttpHeaders headers);

    public String getForXml(String url, HttpHeaders headers);

}

  2.对上面接口实现封装:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import com.alibaba.fastjson.JSONObject;
import com.intf.service.springrest.SpringRestService; @Service("springRestService")
public class SpringRestServiceImpl implements SpringRestService { @Autowired
private RestTemplate restTemplate; private static final Logger LOGGER = LoggerFactory.getLogger(SpringRestServiceImpl.class); @Override
public <T> T postWithJson(String url, JSONObject param, Class<T> responseType) {
// 设置请求头
HttpHeaders headers = new HttpHeaders();
// 设置ContentType
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
// 请求
return postWithJson(url, headers, param, responseType);
} @Override
public <T> T postWithJson(String url, HttpHeaders headers, JSONObject param, Class<T> responseType) {
// 请求
return postWithJson(url, headers, param.toString(), responseType);
} @Override
public <T> T postWithJson(String url, HttpHeaders headers, String param, Class<T> responseType) {
// 设置ContentType
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
// 请求中设置param和headers
HttpEntity<String> request = new HttpEntity<>(param, headers);
// post请求并返回
return restTemplate.postForObject(url, request, responseType);
} @Override
public <T> T getForObject(String url, Class<T> responseType) {
// get请求并返回
return restTemplate.getForObject(url, responseType);
} @Override
public String getForObject(String url, HttpHeaders headers) {
// 设置ContentType
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
// 设置请求头
HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
// get请求获取响应内容
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
// 返回响应body
return response.getBody();
} @Override
public String getForXml(String url, HttpHeaders headers) {
// 设置ACCEPT
headers.add("Accept", MediaType.APPLICATION_XML_VALUE);
// 设置ContentType
headers.setContentType(MediaType.APPLICATION_XML);
// 设置请求头
HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
// get请求获取响应内容
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
// 返回响应body
LOGGER.info(url + "&&&&&" + response.toString() + "&&&&&" + response.getBody());
return response.getBody();
}
}

spring restTemplate 进行http请求的工具类封装的更多相关文章

  1. spring data jpa 动态查询(工具类封装)

    利用JPA的Specification<T>接口和元模型就实现动态查询了.但是这样每一个需要动态查询的地方都需要写一个这样类似的findByConditions方法,小型项目还好,大型项目 ...

  2. Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils

    Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils Spring 系列目录(https://www.cnblogs.com/binary ...

  3. HttpUtils 用于进行网络请求的工具类

    原文:http://www.open-open.com/code/view/1437537162631 import java.io.BufferedReader; import java.io.By ...

  4. 获取Spring容器中Bean实例的工具类(Java泛型方法实现)

    在使用Spring做IoC容器的时候,有的类不方便直接注入bean,需要手动获得一个类型的bean. 因此,实现一个获得bean实例的工具类,就很有必要. 以前,写了一个根据bean的名称和类型获取b ...

  5. HTTP请求客户端工具类

    1.maven 引入依赖 <dependency> <groupId>commons-httpclient</groupId> <artifactId> ...

  6. 发送http请求和https请求的工具类

    package com.haiyisoft.cAssistant.utils; import java.io.IOException;import java.util.ArrayList; impor ...

  7. spring自带的MD5加密工具类

    Spring 自带的md5加密工具类,本来打算自己找一个工具类的,后来想起来Spring有自带的,就翻了翻 //导入包import org.springframework.util.DigestUti ...

  8. Spring 注解(二)注解工具类

    本文转载自Spring 注解(二)注解工具类 导语 首先回顾一下 AnnotationUtils 和 AnnotatedElementUtils 这两个注解工具类的用法: @Test @GetMapp ...

  9. Redis操作字符串工具类封装,Redis工具类封装

    Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...

随机推荐

  1. Redis 设计与实现 4:字典

    Redis 中,字典是基础结构.Redis 数据库数据.过期时间.哈希类型都是把字典作为底层结构. 字典的结构 哈希表 哈希表的实现代码在:dict.h/dictht ,Redis 的字典用哈希表的方 ...

  2. [译] 使用 Espresso 隔离测试视图

    原文地址:Testing Views in Isolation with Espresso 原文作者:Ataul Munim 译文出自:掘金翻译计划 译者:yazhi1992 校对者:lovexiao ...

  3. spring mvc与mybatis与maven+mysql框架整合

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  4. 远程桌面连接(出现身份验证错误。要求的函数不支持)这可能由于CredSSP加密Oracle修正。

    家庭版解决方案 在进行远程桌面时会遇到这种情况.对于Windows 10家庭版用户,是不支持组策略功能的.本文根据官方文档采用修改注册表的方式达到相同的目的. 1.打开注册表   win + R  键 ...

  5. [LeetCode]100. Same Tree判断树相同

    dfs遍历一下判断 public boolean isSameTree(TreeNode p, TreeNode q) { if (p==null) { return q == null; } els ...

  6. MongoDb学习(四)--Repository----语法关键字

    表7.查询方法支持的关键字 关键词 样品 逻辑结果 After findByBirthdateAfter(Date date) {"birthdate" : {"$gt& ...

  7. DNS主从服务器配置实现

    主服务器-centos7-IP:192.168.184.201 从服务器-centos7-IP:192.168.184.202 客户端-ubuntu1804-IP:192.168.184.150 ①客 ...

  8. TIDB简介

    摘自https://pingcap.com/docs-cn/ TiDB 是 PingCAP 公司设计的开源分布式 HTAP (Hybrid Transactional and Analytical P ...

  9. java枚举类学习笔记总结

    枚举类的说明: 1.枚举类的理解:类的对象只有有限个,确定的.我们称此类为枚举类 2.当需要定义一组常量时,强烈建议使用枚举类 3.如果枚举类中只一个对象,则可以作为单例模式的实现方式. 如何自定义枚 ...

  10. 使用IDEA构建Spring Boot项目简单实例

    一.介绍 它的目标是简化Spring应用和服务的创建.开发与部署,简化了配置文件,使用嵌入式web服务器,含有诸多开箱即用的微服务功能,可以和spring cloud联合部署. Spring Boot ...