zoukankan      html  css  js  c++  java
  • RestTemplate

    RestTemplate

    RestTemplate 提供了多种便捷访问远程Http服务的方法, 是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集。

    RestTemplate官网

    需要一个配置类

    package com.fujiew.springcloud.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.client.RestTemplate;
    
    /**
     * @author fujiew
     * @version v1.0.0
     * @Package : com.fujiew.springcloud.config
     * @Description : TODO
     * @Create on : 2020/9/11 11:00
     **/
    @Configuration
    public class ApplicationContextConfig {
        @Bean
        public RestTemplate getRestTemplate() {
            return new RestTemplate();
        }
    }
    
    

    使用

    @RestController
    @Slf4j
    public class PaymentController {
    
        @Resource
        private PaymentService service;
    
        @PostMapping(value = "payment/create")
        public CommonResult<Object> create(@RequestBody Payment payment) {
            int result = service.create(payment);
            log.info("插入结果*****" + result);
            if (result > 0) {
                return new CommonResult<>(200, "插入成功", payment);
            } else {
                return new CommonResult<>(404, "插入失败", null);
            }
        }
    
        @GetMapping(value = "payment/get/{id}")
        public CommonResult<Object> getPaymentById(@PathVariable("id") Long id) {
            Payment payment = service.getPaymentById(id);
            log.info("查询结果" + payment);
            if (payment != null) {
                return new CommonResult<>(200, "查询成功", payment);
            } else {
                return new CommonResult<>(404, "not found", null);
            }
        }
    }
    
    
  • 相关阅读:
    Spring Boot使用@Scheduled定时器任务
    [TaskList] 省选前板子补完计划
    [模板] 计算几何1(基础): 点/向量/线/圆/多边形/其他运算
    网络流刷题日记
    [模板] 网络流相关/最大流ISAP/费用流zkw
    11/5/2018模拟 Problem C
    11/1/2018模拟 Max
    [模板] 笛卡尔树 && RMQ
    bzoj1010-[HNOI2008]玩具装箱toy
    [模板] 斜率优化
  • 原文地址:https://www.cnblogs.com/faddei/p/13650800.html
Copyright © 2011-2022 走看看