当前位置:网站首页>RestTemplate工具类
RestTemplate工具类
2022-08-11 07:51:00 【杨小熊的笔记】
RestTemplate工具类
RestTemplateUtils工具类代码:
package com.example.demo.utils;
import com.example.demo.entity.common.ResultVo;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@Component
public class RestTemplateUtils {
@Resource
private RestTemplate restTemplate;
public <T> ResultVo<T> get(String url, ObjectParameterizedTypeReference<T> responseType) {
return http(url, HttpMethod.GET, null, responseType);
}
public <T> ResultVo<T> post(String url, Object request, ObjectParameterizedTypeReference<T> responseType) {
return http(url, HttpMethod.POST, request, responseType);
}
public <T> ResultVo<T> http(String url, HttpMethod httpMethod, Object request, ObjectParameterizedTypeReference<T> responseType) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Object> requestData = new HttpEntity<>(request, httpHeaders);
return restTemplate.exchange(url, httpMethod, requestData, responseType).getBody();
}
public static class ObjectParameterizedTypeReference <T> extends ParameterizedTypeReference<ResultVo<T>> {
}
}
验证:
package com.example.demo.controller;
import com.example.demo.entity.Book;
import com.example.demo.entity.EmployeeVo;
import com.example.demo.entity.common.ResultVo;
import com.example.demo.exception.DemoException;
import com.example.demo.service.IHelloService;
import com.example.demo.utils.RestTemplateUtils;
import com.example.demo.utils.ResultVoUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@RestController
@Api("Hello 测试接口")
@RequestMapping(value = "hello", produces = MediaType.APPLICATION_JSON_VALUE)
public class HelloController {
private static final Logger LOGGER = LoggerFactory.getLogger(HelloController.class);
@Resource
private RestTemplateUtils restTemplateUtils;
@GetMapping(value = "/request-get")
public ResultVo<Void> requestGet() {
String url = "http://localhost:8888/employee/query/2";
ResultVo<EmployeeVo> resultVo = restTemplateUtils.get(url,
new RestTemplateUtils.ObjectParameterizedTypeReference<>());
LOGGER.info("code: {}", resultVo.getCode());
return ResultVoUtils.success(null);
}
@GetMapping(value = "/request-post")
public ResultVo<Void> requestPost() {
Book book = new Book();
book.setAuthor("吴军");
book.setPublisher("人民邮电出版社");
book.setName("数学之美");
String url = "http://localhost:8888/v1/book/one-book";
ResultVo<Book> resultVo = restTemplateUtils.post(url,
book,
new RestTemplateUtils.ObjectParameterizedTypeReference<>());
LOGGER.info("code: {}", resultVo.getCode());
return ResultVoUtils.success(null);
}
}
ResultVo请参考: 6. 返回统一的Json格式
SpringBoot 学习笔记
Demo 源代码 GitHub地址
1. HelloWorld
2. logback 日志配置
3. 返回 Json 串
4. Tomcat 部署
5. Swagger-ui
6. 返回统一的Json格式
7. 处理全局异常
8. GsonUtils 工具类
9. 多环境部署
10. 集成数据库
11. RestTemplate工具类
边栏推荐
- tf中自减操作;tf.assign_sub()
- TF中的条件语句;where()
- 1096 big beautiful numbers (15 points)
- 经典论文-MobileNet V1论文及实践
- There may be fields that cannot be serialized in the abnormal object of cdc and sqlserver. Is there anyone who can understand it? Help me to answer
- JRS303-数据校验
- 支持各种文件快速重命名最简单的小技巧
- 查找最新人员工资和上上次人员工资的变动情况
- 8、Mip-NeRF
- 麒麟V10系统打包Qt免安装包程序
猜你喜欢
Conditional statements in TF; where()
初级软件测试工程师笔试试题,你知道答案吗?
CSDN21天学习挑战赛——封装(06)
1106 2019数列 (15 分)
Hibernate 的 Session 缓存相关操作
少年成就黑客,需要这些技能
1046 划拳 (15 分)
1091 N-Defensive Number (15 points)
Redis source code-String: Redis String command, Redis String storage principle, three encoding types of Redis string, Redis String SDS source code analysis, Redis String application scenarios
【云原生】云原生在网络安全领域的应用
随机推荐
【LeetCode】Summary of linked list problems
Test cases are hard?Just have a hand
美术2.4 UV原理基础
Serverless + domain name can also build a personal blog? Really, and soon
如何仅更改 QGroupBox 标题的字体?
The most complete documentation on Excel's implementation of grouped summation
1046 punches (15 points)
项目2-年收入判断
关于Excel实现分组求和最全文档
1002 写出这个数 (20 分)
FPGA 20个例程篇:11.USB2.0接收并回复CRC16位校验
Conditional statements in TF; where()
jenkins简单使用
tf.reduce_mean() and tf.reduce_sum()
选择收银系统主要看哪些方面?
Break pad source code compilation--refer to the summary of the big blogger
TF中的条件语句;where()
9、Neural Sparse Voxel Fields
There may be fields that cannot be serialized in the abnormal object of cdc and sqlserver. Is there anyone who can understand it? Help me to answer
About # SQL problem: how to set the following data by commas into multiple lines, in the form of column display