• RestTemplate工具类


    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>> {}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    验证:

    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);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    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工具类

  • 相关阅读:
    【数据结构】HashSet的底层数据结构
    锁的概念!
    MATLAB神经网络编程(一)——感知器
    maven exclude 排除文件夹
    迭代加深算法题目
    浅谈few-shot
    【配电网重构】负荷平衡的配电网重构【含Matlab源码 2180期】
    如何搬运视频赚钱?
    【多任务案例:猫狗脸部定位与分类】
    ESP8266/ESP32 +1.3“ or 0.96“ IIC OLED指针时钟+数字时钟显示
  • 原文地址:https://blog.csdn.net/Next_Second/article/details/126239881