Java Http请求方式之RestTemplate常用方法详解
作者:Real_ma 发布时间:2021-06-29 02:32:51
标签:Java,Http,请求方式,RestTemplate
引言
在开发中有时候经常需要一些Http请求,请求数据,下载内容,也有一些简单的分布式应用直接使用Http请求作为跨应用的交互协议。
在Java中有不同的Http请求方式,主要就是HttpURLConnection或者ApacheHttpClient,但是这两个用起来都感觉有那么一点点的复杂;
好在Spring内置了RestTemplate作为Http请求的工具类,简化了很多操作,虽然Spring5推出了WebClient,但是整体感觉还是RestTemplate用起来更简单方便一些。
这里记录分享下RestTemplate的常见使用方式,RestTemplate作为Java中最简单好用的Http请求工具类一定要了解一下
常见用法
简单Get\Post请求
@Test
public void testGetPost(){
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new FastJsonHttpMessageConverter());
String res = restTemplate.postForObject("http://test.aihe.space/", null, String.class);
System.out.println(res);
String res2 = restTemplate.getForObject("http://test.aihe.space/", String.class);
System.out.println(res2);
}
Post提交常规表单
@Test
public void testPostFrom(){
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
map.add("id", "1");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
String fooResourceUrl = "http://test.aihe.space/";
ResponseEntity<String> response = restTemplate.postForEntity(fooResourceUrl, request, String.class);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
}
Post上传文件
注意:上传文件时的value为FileSystemResource
@Test
public void testPostFile(){
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
map.add("id", "1");
map.add("file",new FileSystemResource("/Users/aihe/code/init/src/test/java/me/aihe/RestTemplateTest.java"));
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
String fooResourceUrl = "http://test.aihe.space/";
ResponseEntity<String> response = restTemplate.postForEntity(fooResourceUrl, request, String.class);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
}
配置项
请求添加Cookie\Header
@Test
public void testCookieHeader(){
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
// 常见的Header都可以直接设置
// headers.set
headers.set("custom1","customValue1");
// 设置Cookie
headers.set(HttpHeaders.COOKIE,"xxxx");
map.add("id", "1");
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
String fooResourceUrl = "http://test.aihe.space/";
ResponseEntity<String> response = restTemplate.postForEntity(fooResourceUrl, request, String.class);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
}
配置请求工厂 超时、代理
使用Rest请求的时候注意设置超时时间
@Test
public void testHttpFactory(){
RestTemplate restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setReadTimeout(5000);
requestFactory.setConnectTimeout(3000);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 7890));
requestFactory.setProxy(proxy);
restTemplate.setRequestFactory(requestFactory);
restTemplate.getMessageConverters().add(new FastJsonHttpMessageConverter());
String res = restTemplate.postForObject("http://test.aihe.space/", null, String.class);
System.out.println(res);
}
配置 * 、转换器,错误处理
@Test
public void testConverterInterceptor(){
RestTemplate restTemplate = new RestTemplate();
restTemplate.setInterceptors(Collections.singletonList(new BasicAuthenticationInterceptor("admin","admin")));
restTemplate.getMessageConverters().add(new FastJsonHttpMessageConverter());
restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
String res = restTemplate.postForObject("http://test.aihe.space/", null, String.class);
System.out.println(res);
}
错误重试(额外)
可以考虑使用Spring Retry,但是相当于引入了新的东西,如果没有特殊必要,可以自己简单用for循环做下;
SSL请求
参考:stackoverflow.com/questions/1…
@Test
public void testSSL() throws FileNotFoundException, NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException {
String keyStorePassword = "123456";
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(new FileInputStream(new File("keyStoreFile")),
keyStorePassword.toCharArray());
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
new SSLContextBuilder()
.loadTrustMaterial(null, new TrustSelfSignedStrategy())
.loadKeyMaterial(keyStore, keyStorePassword.toCharArray())
.build(),
NoopHostnameVerifier.INSTANCE);
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(
socketFactory).build();
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
String res = restTemplate.postForObject("http://test.aihe.space/", null, String.class);
System.out.println(res);
}
基于RestTemplate一些工具
钉钉机器人通知
可以支持发送普通文本、ActionCard,Markdown的消息
import java.util.ArrayList;
import java.util.List;
import lombok.Data;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
/**
* 使用场景:
* 功能描述:https://developers.dingtalk.com/document/app/custom-robot-access/title-72m-8ag-pqw
*/
@Slf4j
public class RobotUtils {
private static RestTemplate restTemplate = new RestTemplate();
private static String URL = "";
public static void main(String[] args) {
sendMarkDownMsg(
URL,
"测试",
"测试",
new ArrayList<>()
);
}
/**
* {
* "msgtype": "text",
* "text": {
* "content": "我就是我, @150XXXXXXXX 是不一样的烟火"
* },
* "at": {
* "atMobiles": [
* "150XXXXXXXX"
* ],
* "isAtAll": false
* }
* }
*/
public static void sendTextMsg(String url, String content, List<String> atPerson) {
RobotMsg robotMsg = new RobotMsg();
robotMsg.setMsgtype("text");
RobotAt robotAt = new RobotAt();
robotAt.setAtAll(false);
if (atPerson != null && atPerson.size() > 0) {
robotAt.setAtMobiles(atPerson);
}
robotMsg.setAt(robotAt);
RobotText robotText = new RobotText();
robotText.setContent(content);
robotMsg.setText(robotText);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, robotMsg, String.class);
log.info("sendTextMsg {}",responseEntity.getBody());
}
/**
* @param url 机器人地址
* @param title actionCard标题
* @param text 缩略内容
* @param vertical 按钮方向
* @param btns 按钮内容
*/
public static void sendActionCardMsg(String url, String title, String text, Boolean vertical, List<RobotBtn> btns) {
RobotMsg robotMsg = new RobotMsg();
robotMsg.setMsgtype("actionCard");
RobotAt robotAt = new RobotAt();
robotAt.setAtAll(false);
RobotActionCard robotActionCard
= new RobotActionCard();
robotActionCard.setTitle(title);
robotActionCard.setText(text);
robotActionCard.setBtnOrientation((vertical == null || vertical) ? "0" : "1");
robotActionCard.setBtns(btns);
robotMsg.setActionCard(robotActionCard);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, robotMsg, String.class);
System.out.println(responseEntity.getBody());
}
public static void sendMarkDownMsg(String url,String title,String text,List<String> atPerson){
RobotMarkDownMsg markDownMsg = new RobotMarkDownMsg();
markDownMsg.setMsgtype("markdown");
RobotAt robotAt = new RobotAt();
robotAt.setAtAll(false);
if (atPerson != null && atPerson.size() > 0) {
robotAt.setAtMobiles(atPerson);
}
markDownMsg.setAt(robotAt);
RobotMarkdownText markdownText = new RobotMarkdownText();
markdownText.setTitle(title);
markdownText.setText(text);
markDownMsg.setMarkdown(markdownText);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, markDownMsg, String.class);
System.out.println(responseEntity.getBody());
}
public enum SupportRobotEnum {
/**
* 测试机器人群
*/
CESHI("测试机器人群",
"");
@Getter
private String desc;
@Getter
private String url;
SupportRobotEnum(String desc, String url) {
this.desc = desc;
this.url = url;
}
}
@Data
public static class RobotAt {
public List<String> atMobiles;
public boolean isAtAll;
}
@Data
public static class RobotMarkDownMsg{
public String msgtype;
public RobotAt at;
public RobotMarkdownText markdown;
}
@Data
public static class RobotMarkdownText {
String title;
String text;
}
@Data
public static class RobotMsg {
public String msgtype;
public RobotAt at;
/**
* 普通文字消息类型消息
*/
public RobotText text;
/**
* actionCard类型消息时支持
*/
public RobotActionCard actionCard;
}
@Data
public static class RobotText {
public String content;
}
@Data
public static class RobotActionCard {
public String title;
public String text;
public String hideAvatar;
public String btnOrientation;
public List<RobotBtn> btns;
}
@Data
public static class RobotBtn {
public String title;
public String actionURL;
public static RobotBtn buildBtn(String title, String actionUrl) {
RobotBtn robotBtn = new RobotBtn();
robotBtn.setActionURL(actionUrl);
robotBtn.setTitle(title);
return robotBtn;
}
}
}
来源:https://juejin.cn/post/7147334077740220447
0
投稿
猜你喜欢
- 目前,比较常用的实现Java导入、导出Excel的技术有两种Jakarta POI和Java Excel直接上代码:一,POIPOI是apa
- 延迟加载1 使用延迟加载意义在进行数据查询时,为了提高数据库查询性能,尽量使用单表查询,因为单表查询比多表关联查询速度要快。如果查询单表就可
- 前言现在开发大部分都是服务化或者微服务,数据交换都是跨服务的,这里记录java调取其他接口的方法,下面话不多说了,来一起看看详细的介绍吧。j
- 双向链表(Doubly linked list)什么是双向链表?双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直
- 概述为文档添加必要的批注可以给文档使用者提供重要的提示信息,下面的示例中,将介绍通过C#编程语言来给Excel表格中的指定单元格内容添加批注
- 对列表进行排序也是我们经常遇到的问题,这里缩小一下范围,使用map来对列表排序。相信大家都有过TreeMap排序的经历,不过Map.Entr
- 在Java中,可以通过Runtime类或ProcessBuilder类来实现调用外部程序。Runtime类与ProcessBuilder类使
- 可以用DSA和RSA,如: using System; using System.Text; using Sy
- 封装(Encapsulation)是面向对象编程的一个核心概念,它意味着将数据(属性)和方法(操作数据的函数)捆绑在一起,形成一个类(Cla
- 什么是MybatisMyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software fou
- ModbusModbus是一种串行通信协议。Modbus 一个工业上常用的通讯协议、一种通讯约定。Modbus协议包括RTU、ASCII、T
- 任何一个类都是Class类的实例对象,这个实例对象有三种表示方式第一种表示方式(任何一个类都有一个隐含的静态成员变量class):Class
- 前言自从国产之光fastjson频频暴雷,jackson json的使用是越来越广泛了。尤其是spring家族把它搞成了默认的JSON处理包
- Java类的加载说明Java类的编译代码都存在于它自己的独立文件中(class),该文件只在需要使用程序代码时才会被加载。类加载在创建类的第
- 第一部分:Java数据结构要理解Java数据结构,必须能清楚何为数据结构?数据结构:Data_Structure,它是储存数据的一种结构体,
- 泛型 一般 出现在集合中,迭代器中 也会出现!泛型 是为了 提高代码的 安全性。 泛型 确保数据类型的唯一性。在我们常用的容器中
- 概述常用的弹窗有菜单,或者Dialog,但更加人性化和可自定义的还是PopupWindow如果只是展示列表数据或者弹窗列表选择,直接使用Li
- 引言:前面几个专题对委托进行了详细的介绍的,然后我们在编写代码过程中经常会听到“事件”这个概念的,尤其是写UI的时候,当我们点击一个按钮后V
- 本文实例讲述了Android编程实现系统重启与关机的方法。分享给大家供大家参考,具体如下:最近在做个东西,巧合碰到了sharedUserId
- 一、项目运行环境配置:Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,