付给招聘网站的费用怎么做分录,河南郑州做网站汉狮,有哪些炫酷的官方网站,企业管理软件都有哪些背景 在查询订单信息的时候#xff0c;需要获取用户的信息#xff0c;同时订单和用户分属于不同的服务中#xff0c;并且服务的数据库的数据分开的#xff0c;其直接连接数据库并操作数据库是不可以的。那我们可以通过RestTemplate对象请求另一个服务的API接口获取相关的响…背景 在查询订单信息的时候需要获取用户的信息同时订单和用户分属于不同的服务中并且服务的数据库的数据分开的其直接连接数据库并操作数据库是不可以的。那我们可以通过RestTemplate对象请求另一个服务的API接口获取相关的响应数据然后再封装返回在Spring Boot中我们可以先注册RestTemplate的Bean package com.app.order.config;import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;/*** webmvc的相关配置** author Administrator*/
Configuration
Slf4j
public class WebMvcConfig {/*** 注入RestTemplate的Bean** return 返回RestTemplate*/Beanpublic RestTemplate restTemplate() {return new RestTemplate();}}在使用的地方注入RestTemplate对象 /*** 结合RequiredArgsConstructor进行构造器注入*/private final RestTemplate restTemplate; 在查询的方法处使用远程调用 /*** 根据id查询订单信息** param id 订单id* return 订单信息*/GetMapping(/{id})public ResultBeanOrderVo getById(PathVariable Long id) {log.info(根据id查询订单信息...);Order order orderService.getById(id);if (order ! null) {OrderVo orderVo new OrderVo();BeanUtil.copyProperties(order, orderVo);// 远程查找用户服务获取用户名信息// url地址String url http://127.0.0.1:8080/users/ order.getUserId();// 发起远程调用ResultBean resultBean restTemplate.getForObject(url, ResultBean.class);if (resultBean ! null) {UserVo userVo new UserVo();BeanUtil.copyProperties(resultBean.getData(), userVo);orderVo.setUsername(userVo.getUsername());}return ResultBean.success(orderVo);}return ResultBean.error(没有查询到对应订单信息);}