spring restTemplate 进行http请求的工具类封装
本文为博主原创,未经允许不得转载:
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请求的工具类封装的更多相关文章
- spring data jpa 动态查询(工具类封装)
利用JPA的Specification<T>接口和元模型就实现动态查询了.但是这样每一个需要动态查询的地方都需要写一个这样类似的findByConditions方法,小型项目还好,大型项目 ...
- Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils
Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils Spring 系列目录(https://www.cnblogs.com/binary ...
- HttpUtils 用于进行网络请求的工具类
原文:http://www.open-open.com/code/view/1437537162631 import java.io.BufferedReader; import java.io.By ...
- 获取Spring容器中Bean实例的工具类(Java泛型方法实现)
在使用Spring做IoC容器的时候,有的类不方便直接注入bean,需要手动获得一个类型的bean. 因此,实现一个获得bean实例的工具类,就很有必要. 以前,写了一个根据bean的名称和类型获取b ...
- HTTP请求客户端工具类
1.maven 引入依赖 <dependency> <groupId>commons-httpclient</groupId> <artifactId> ...
- 发送http请求和https请求的工具类
package com.haiyisoft.cAssistant.utils; import java.io.IOException;import java.util.ArrayList; impor ...
- spring自带的MD5加密工具类
Spring 自带的md5加密工具类,本来打算自己找一个工具类的,后来想起来Spring有自带的,就翻了翻 //导入包import org.springframework.util.DigestUti ...
- Spring 注解(二)注解工具类
本文转载自Spring 注解(二)注解工具类 导语 首先回顾一下 AnnotationUtils 和 AnnotatedElementUtils 这两个注解工具类的用法: @Test @GetMapp ...
- Redis操作字符串工具类封装,Redis工具类封装
Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...
随机推荐
- RestTemplate发起http请求中文乱码问题解决方案
RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); MediaType t ...
- 使用@Cacheable注解时,Redis连不上,直接调用方法内部的解决方案
最近redis 域名一致解析错误,导致业务多了很多异常.那么如何在这种情况下直接访问数据库,而不是报错呢 1. 解决方案 其实很简单,在配置 redis 时,只需要多一项配置,继承 CachingCo ...
- java中使用IO流将以文件中的内容去取到指定的文件中
public class Demo12 { public static void main(String[] args) throws IOException { File file=new File ...
- jsp中将一个jsp引入另一个jsp指定位置
<jsp:include page="badSurveyUpdate.jsp"/>
- java中对list集合中的数据按照某一个属性进行分组
import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Ite ...
- javap使用
在反编译前你当然需要先编译这个类了进入当前目录下:javac -g SynchronizedTest.java(使用-g参数是因为要得到下面javap -l时的输出需要使用此选项) 编译完成后,我们在 ...
- [leetcode]TwoSum系列问题
1.普通数组找两个数,哈希表建立数值和下标的映射,遍历时一边判断一边添加 /* 哇,LeetCode的第一题...啧啧 */ public int [] twoSum(int[] nums, int ...
- .NETCore使用EntityFrameworkCore连接数据库生成实体
EF Core 通过数据库提供程序插件模型与 SQL Server/SQL Azure.SQLite.Azure Cosmos DB.MySQL.PostgreSQL 和更多数据库配合使用. 使用EF ...
- 关于STM32的CAN的过滤器
关于STM32的CAN的过滤器STM32普通型芯片的CAN有14组过滤器组,互联型有28组过滤器组.一般我们用的都是普通型的,所以在本文中可以说STM32有14组过滤器组.根据配置,每1组过滤器组可以 ...
- 数据库零基础之---了解数据库的事务[ACID]
事务指逻辑上的一组操作,组成这组操作的各个单元,要不全部成功,要不全部不成功. 我们先举一个例子来描述一下事务: 假设要张三通过银行给李四进行转账1000元钱,张三原有余额10000元整,李四有人民币 ...