4.3 修改pom添加依赖
<dependencies>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
4.4编写yml文件
server:
port: 80
spring:
application:
name:springcloud-consumer-order-service
4.5编写启动类
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
}
4.6建立实体
package cn.bdqn.domain;
import java.io.Serializable;
public class Payment implements Serializable {
private Integer id;
private String flowNumber;
public void setId(Integer id) {
this.id = id;
}
public void setFlowNumber(String flowNumber) {
this.flowNumber = flowNumber;
}
public Integer getId() {
return id;
}
public String getFlowNumber() {
return flowNumber;
}
}
4.7编写响应结果的Bean
package cn.bdqn.bean;
public class ResponseResult <T> {
//响应的编码
private Integer code;
//响应给前段用户的消息提示
private String message;
//响应的数据
private T data;
public ResponseResult() {
}
public ResponseResult(Integer code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
}
public Integer getCode() {
return code;
}
public String getMessage() {
return message;
}
public T getData() {
return data;
}
public void setCode(Integer code) {
this.code = code;
}
public void setMessage(String message) {
this.message = message;
}
public void setData(T data) {
this.data = data;
}
}
4.8 RestTemplate注册到spring
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
4.9编写OrderApplication控制器
public class OrderController {
private static final String PAYMENT_URL="http://localhost:8001";
@Autowired
private RestTemplate restTemplate;
//根据id查询
@GetMapping("/consumer/payment/get/{id}")
public ResponseResult queryById(@PathVariable("id") Integer id){
ResponseResult rs =restTemplate.getForObject(PAYMENT_URL+"/payment/id"+id,ResponseResult.class);
return rs;
}
//创建订单
@GetMapping("/consumer/payment/save")
public ResponseResult save(Payment payment){
ResponseResult rs =restTemplate.postForObject(PAYMENT_URL+"/payment/save", payment,ResponseResult.class);
return rs;
}
-
4.11问题
- 因为
支付提供者端的代码如下
@PostMapping("/payment/save")
public ResponseResult save(@RequestBody Payment payment){
try {
paymentService.save(payment);
return new ResponseResult(200,"成功",null);
}catch (Exception e){
e.printStackTrace();
return new ResponseResult(500,"失败",null);
}
}