当前位置:网站首页>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工具类
边栏推荐
- 2.1 - Gradient Descent
- 【415. 字符串相加】
- 1106 2019 Sequence (15 points)
- cdc连sqlserver异常对象可能有无法序列化的字段 有没有大佬看得懂的 帮忙解答一下
- Creo9.0 特征的成组
- 软件测试常用工具的用途及优缺点比较(详细)
- 支持各种文件快速重命名最简单的小技巧
- My creative anniversary丨Thank you for being with you for these 365 days, not forgetting the original intention, and each is wonderful
- 【C语言】每日一题,求水仙花数,求变种水仙花数
- Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!
猜你喜欢

8、Mip-NeRF

1051 复数乘法 (15 分)

机器学习(二)线性回归

The softmax function is used in TF;

The most complete documentation on Excel's implementation of grouped summation

1091 N-Defensive Number (15 points)

Decrement operation in tf; tf.assign_sub()

1003 I want to pass (20 points)

【TA-霜狼_may-《百人计划》】图形3.7.2 command buffer简

Mysql JSON对象和JSON数组查询
随机推荐
C语言-结构体
无服务器+域名也能搭建个人博客?真的,而且很快
string类接口介绍及应用
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: 69. Square root of x
TF中的One-hot
oracle数据库中列转行,列会有变化
关于#sql#的问题:怎么将下面的数据按逗号分隔成多行,以列的形式展示出来
零基础SQL教程: 主键、外键和索引 04
2022-08-10 mysql/stonedb-慢SQL-Q16-耗时追踪
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
Tensorflow中使用tf.argmax返回张量沿指定维度最大值的索引
项目1-PM2.5预测
1076 Wifi密码 (15 分)
TF generates (feature, label) set through feature and label, tf.data.Dataset.from_tensor_slices
Test cases are hard?Just have a hand
【实战系列】OpenApi设计规范
1.1-Regression
1106 2019 Sequence (15 points)