public static final String PaymentSrv_URL = "http://localhost:8001";
@Resource //也可以写@Autowired,但是这个注解有时候会出错
private RestTemplate restTemplate;
// payDTO是要真正需要操作的内容,ResultData 是规定返回的格式
增
@GetMapping("/consumer/pay/add")
public ResultData addOrder(PayDTO payDTO){
return restTemplate.postForObject(PaymentSrv_URL + "/pay/add",payDTO,ResultData.class);
}
删
@GetMapping("/consumer/pay/del/{id}")
public ResultData delPayInfo(@PathVariable("id") Integer id){
//使用delete发送DELETE请求,返回值为void,方法public后换成void
//restTemplate.delete(PaymentSrv_URL + "/pay/del/"+id,99);
// 使用exchange发送DELETE请求,有返回值
ResponseEntity<ResultData> result = restTemplate.exchange(PaymentSrv_URL + "/pay/del/"+id, HttpMethod.DELETE,null,ResultData.class);
return result.getBody();
}
改
@GetMapping( "/consumer/pay/update")
public ResultData updatePayInfo(PayDTO payDTO){
//restTemplate.put(PaymentSrv_URL + "/pay/update",payDTO, 99);
// 请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
// 发送请求
HttpEntity<PayDTO> httpEntity = new HttpEntity<>(payDTO, headers);
ResponseEntity<ResultData> result = restTemplate.exchange(PaymentSrv_URL + "/pay/update", HttpMethod.PUT,httpEntity,ResultData.class);
return result.getBody();
}
查
@GetMapping("/consumer/pay/get/{id}")
public ResultData getPayInfo(@PathVariable("id") Integer id){
return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/"+id, ResultData.class, id);
}
postman验证