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);
}
}
文章来源:https://www.toymoban.com/news/detail-610028.html
文章来源地址https://www.toymoban.com/news/detail-610028.html
到了这里,关于三,创建订单微服务消费者 第三章的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!