当前位置: 首页 > news >正文

Java RestTemplate 通用请求工具类

在现代Java应用中,与外部服务进行HTTP通信是非常常见的需求。本文将介绍一个实用的工具类RestTemplateUtils,它基于Spring框架的RestTemplate,简化了各种HTTP请求的处理。

工具类概述

RestTemplateUtils提供了三种主要的HTTP请求处理方法:

  1. 通用JSON请求方法:处理常规的JSON格式请求

  2. 扩展的通用JSON请求方法:支持任意对象作为请求体

  3. 文件上传方法:专门处理multipart/form-data格式的文件上传

1. 通用工具类

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;import java.util.Map;@Slf4j
public class RestTemplateUtils {/*** 通用HTTP请求方法** @param requestBody 请求体内容* @param url 完整请求url* @param httpMethod 请求方法类型 (GET, POST, PUT, DELETE等)* @return 响应内容字符串,失败时返回null*/public static String executeRequest(Map<String, String> requestBody, String url, HttpMethod httpMethod) {RestTemplate restTemplate = new RestTemplate();try {// 请求头HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);// 请求体HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(requestBody, headers);// 服务端地址、请求方法、请求实体(包含头和体、响应类型ResponseEntity<String> response = restTemplate.exchange(url, httpMethod, requestEntity, String.class);if (!response.getStatusCode().is2xxSuccessful()) {throw new RuntimeException("HTTP请求失败,状态码: " + response.getStatusCode());}return response.getBody();} catch (Exception e) {log.error("HTTP请求失败: {} {}, 错误: {}", httpMethod, url, e.getMessage());return null;}}/*** 通用HTTP请求方法(支持任意请求体类型)** @param requestBody 请求体内容,可以是Map、POJO或任何可序列化为JSON的对象* @param url         完整的请求URL(包含协议、主机、路径和参数)* @param httpMethod  请求方法类型(GET/POST/PUT/DELETE等)* @return 响应内容的字符串形式* @throws RuntimeException 当HTTP请求失败或返回非2xx状态码时抛出*/public static String executeRequest(Object requestBody, String url, HttpMethod httpMethod) {RestTemplate restTemplate = new RestTemplate();try {// 请求头HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);// 请求体HttpEntity<Object>  requestEntity = new HttpEntity<>(requestBody, headers);// 服务端地址、请求方法、请求实体(包含头和体、响应类型ResponseEntity<String> response = restTemplate.exchange(url, httpMethod, requestEntity, String.class);if (!response.getStatusCode().is2xxSuccessful()) {throw new RuntimeException("HTTP请求失败,状态码: " + response.getStatusCode());}return response.getBody();} catch (Exception e) {log.error("HTTP请求失败: {} {}, 错误: {}", httpMethod, url, e.getMessage());return null;}}/*** 文件上传专用方法 (multipart/form-data)** @param file       上传的文件* @param formData   其他表单字段* @param url        目标URL* @return 响应内容*/public static String uploadFile(MultipartFile file, Map<String, String> formData, String url, String fileName) {RestTemplate restTemplate = new RestTemplate();try {// 1. 构建 MultipartBodyHttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.MULTIPART_FORM_DATA);  // 关键!if (StringUtils.isNotEmpty(fileName)) {headers.set("accept", "application/json");  // 添加 accept 头}MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();// 设置文件名fileName = StringUtils.isEmpty(fileName)? "file": fileName;// 添加文件body.add(fileName, new ByteArrayResource(file.getBytes()) {@Overridepublic String getFilename() {return file.getOriginalFilename();  // 保留原始文件名}});// 添加其他表单字段(如果有)if (formData != null) {formData.forEach(body::add);}// 2. 发送请求HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);if (!response.getStatusCode().is2xxSuccessful()) {throw new RuntimeException("上传失败,状态码: " + response.getStatusCode());}return response.getBody();} catch (Exception e) {log.error("文件上传失败: {}, 错误: {}", url, e.getMessage());return null;}}
}

2. 使用示例

    public AjaxResult retrieve() {try {//  使用Map构建请求体Map<String, String> requestBody = new HashMap<>();requestBody.put("user_id", "1");// 发送POST请求String bodyStr = RestTemplateUtils.executeRequest(requestBody, "url", HttpMethod.POST);// 解析JSON响应ObjectMapper objectMapper = new ObjectMapper();Map<String, Object> responseMap = objectMapper.readValue(bodyStr, new TypeReference<Map<String, Object>>() {});// 检查状态码Integer status = (Integer) responseMap.get("status");Object data = responseMap.get("data");if (status != null && status == 200) {return AjaxResult.success(data);}} catch (Exception e) {e.printStackTrace();}return AjaxResult.success(null);}

优点总结

  1. 简化代码:封装了重复的HTTP请求处理逻辑

  2. 统一错误处理:集中处理异常和错误状态码

  3. 类型安全:明确区分JSON请求和文件上传

  4. 灵活性:支持各种HTTP方法和请求体类型

  5. 易于维护:所有HTTP请求相关代码集中在一处

使用建议

  1. 对于简单的键值对请求,使用第一个方法

  2. 对于复杂对象请求,使用第二个方法

  3. 文件上传必须使用第三个专用方法

  4. 在生产环境中,可以考虑添加重试机制和更详细的日志记录

这个工具类非常适合中小型项目,可以显著减少HTTP请求相关的样板代码,让开发者更专注于业务逻辑的实现。

一线网资源-全网一站式平台

http://www.xdnf.cn/news/591787.html

相关文章:

  • 2024游戏安全白皮书:对抗激烈!PC游戏外挂功能数增长超149%,超85%移动外挂为定制挂(附获取方式)
  • 基于阿里云DashScope API构建智能对话指南
  • 写一个计划任务脚本(定时执行)
  • PostgreSQL跨数据库表字段值复制实战经验分
  • 对于从事FPGA行业的人来说,需要掌握哪些知识
  • ant design 日历组件a-calendar如何汉化
  • 二分算法的补充说明
  • 表格单元格多行文本溢出写法
  • 基于SpringBoot的美食分享平台设计与开发(Vue MySQL)
  • 高效数据库管理新体验:SQLynx 3.7 功能解析与团队协作场景实践
  • 06算法学习_58. 区间和
  • PrimeVue菜单组件深度解析:构建高效能的Web导航系统
  • 3 tomcat原理
  • 多元回归的假设检验
  • Linux中 I/O 多路复用机制的边缘触发与水平触发
  • 鸿蒙运动开发:计算户外运动步频与步幅,与地图路线绘制
  • 链表-环形链表||
  • 3.8.2 利用RDD计算总分与平均分
  • Java 多线程编程:解锁高性能应用开发的密钥
  • RAG系统实战:文档切割与转换核心技术解析
  • Golang 访问 map 中的结构体字段时如何避免拷贝
  • 无anaconda搭建yolo11环境
  • 鸿蒙进阶——CMakelist、GN语法简介及三方库通用移植指南
  • 技术篇-2.3.Golang应用场景及开发工具安装
  • 晶振选型三大陷阱:工作温度、电压与负载电容的隐藏矛盾
  • 【AT32】 at32 软复位
  • mssql查询历史执行过的语句日志
  • 提示词工程驱动Mermaid图表生成:技术原理与实战案例
  • 力扣面试150题-- 二叉树展开为链表
  • MYSQL备份与恢复