20201214-springboot学习

springboot学习整理

一:使用RestTemplate

1.配置类

@Configuration
public class RestConfig {
    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

2.使用类

@RestController
@Slf4j
public class PaymentController {

    public static final String PROVIDE_URL = "http://localhost:7001";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping(value = "/consumer/payment/create")
    public CommonResult save(Payment payment) {
        return restTemplate.postForObject(PROVIDE_URL + "/payment/create", payment, CommonResult.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
        return restTemplate.getForObject(PROVIDE_URL + "/payment/get/" + id, CommonResult.class);
    }

}

3.调用类

注意: 这里接收参数必须加上@RequestBody

    @PostMapping(value = "/payment/create")
    public CommonResult save(@RequestBody Payment payment) {
        int result = paymentService.insert(payment);
        log.info("*****插入结果:" + result);
        if (result > 0) {
            return new CommonResult<>(200, "插入数据库成功", result);
        } else {
            return new CommonResult<>(444, "插入数据库失败");
        }
    }
上一篇:Ribbon负载均衡


下一篇:Sentinel之服务熔断与限流