使用RestTemplate的postForObject方法直接传递对象

用户支付成功后,我们要向中间商的系统打款,【订单支付成功接口】请求内部的【消息推送接口】,通过Rabbitmq消息通知中间商的系统,他们返回打款是否成功,这里展示的是【订单支付成功接口】向【消息推送接口】的请求。

@RequestBody这个注解很重要,一定要加这个注解,否则请用户求对象huaBoOrderPushRequest对象的字段都为null。

【消息推送接口】

/**
 * @author johny
 * @date 2020/5/13
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @PostMapping("/push")
    public ResultData updateOrderStatus(@RequestBody HuaBoOrderPushRequest huaBoOrderPushRequest){
        HuaBoOrderPushDomainFactory huaBoOrderPushDomainFactory = new  HuaBoOrderPushDomainFactory();
        HuaBoOrderPushDomain huaBoOrderPushDomain = huaBoOrderPushDomainFactory.compose(huaBoOrderPushRequest);
        try {
            orderService.updateOrderByHuaBoOrderPush(huaBoOrderPushDomain);
        }catch (Exception e){
            log.error("更改订单支付状态失败:",e);
            return ResultData.getFailResult(e.getMessage());
        }
        return ResultData.getSuccessResult();
    }
}

【订单支付成功接口】

@Autowired
    RestTemplate restTemplate;

    /**
     * 主订单号	orderId
     * 客户订单号	outTradeId
     * 订单状态	orderStatus
     * 错误消息	cause
     * 签名	sign
     */
    public ResultData orderPush(HuaboOrderPushRequest huaboOrderPushRequest) throws MalformedURLException {
        ......

        ResponseEntity resp = restTemplate.postForEntity(PropertyUtil.getProperty("huabo.order.push"), huaboOrderPushRequest, ResultData.class);

        return resp.getBody();
    }

 

你可能感兴趣的:(java,RestTemplate,spring,boot,电商系统,系统架构)