因为需要用到RestFual但是在 穿值问题上有点毛病寻找了半天才找到解决方案,
首先这里需要注入一个bean
@Configuration public class ApplicationContextConfig { @Bean public RestTemplate GetRestTemplate(){ return new RestTemplate(); } }然后在controller上引用 这个Controller是对外部访问提供的;
@Resource private RestTemplate restTemplate; @GetMapping("/pay/save") public CommonResult<Payment> create(Payment payment){ return restTemplate.postForObject(paymentUrl + "/pay/save",payment,CommonResult.class); }这个controller是对内部提供的:
@RestController @Slf4j public class PayMentController { @Resource private PayMentService payMentService; @PostMapping(value = "/pay/save") public CommonResult save(@RequestBody Payment payment){ //如果直接调用Rest if (payment.getSerial() == null){ return new CommonResult<Payment>(501,"失败",null); } int i = payMentService.save(payment); if (i > 0){ return new CommonResult<Payment>(200,"成功",null); }else { return new CommonResult<Payment>(500,"失败",null); } } }如果我们没对 内部提供的接口提供@RequestBody,那么内部接口是接收不到值的,如果要是不想加这个注解也可以,那就比较麻烦点,就需要对数据进行封装,像如下写法比较麻烦 对外提供的接口
@GetMapping("/pay/save") public CommonResult<Payment> create(Payment payment){ MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>(); paramMap.add("serial", payment.getSerial()); return restTemplate.postForObject(paymentUrl + "/pay/save",paramMap,CommonResult.class); }二选一