?
?4.3 修改pom添加依賴(lài)
<dependencies>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--監(jiān)控-->
<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>
<!--測(cè)試-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
?4.4編寫(xiě)yml文件
server:
port: 80
spring:
application:
name:springcloud-consumer-order-service
4.5編寫(xiě)啟動(dòng)類(lèi)
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
}
4.6建立實(shí)體
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編寫(xiě)響應(yīng)結(jié)果的Bean
package cn.bdqn.bean;
public class ResponseResult <T> {
//響應(yīng)的編碼
private Integer code;
//響應(yīng)給前段用戶(hù)的消息提示
private String message;
//響應(yīng)的數(shù)據(jù)
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注冊(cè)到spring
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
4.9編寫(xiě)OrderApplication控制器
public class OrderController {
private static final String PAYMENT_URL="http://localhost:8001";
@Autowired
private RestTemplate restTemplate;
//根據(jù)id查詢(xún)
@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;
}
//創(chuàng)建訂單
@GetMapping("/consumer/payment/save")
public ResponseResult save(Payment payment){
ResponseResult rs =restTemplate.postForObject(PAYMENT_URL+"/payment/save", payment,ResponseResult.class);
return rs;
}
-
4.11問(wèn)題
- 因?yàn)?img src="https://imgs.yssmx.com/Uploads/2023/07/610028-12.png" alt="三,創(chuàng)建訂單微服務(wù)消費(fèi)者 第三章,微服務(wù),架構(gòu),云原生" referrerpolicy="no-referrer" />
?支付提供者端的代碼如下
@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);
}
}
?
?
?
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-610028.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-610028.html
到了這里,關(guān)于三,創(chuàng)建訂單微服務(wù)消費(fèi)者 第三章的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!