【springcloud合集】05,java架构设计书籍推荐

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns=“http://maven.apache.org/POM/4.0.0”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>

cloud2020

com.li.springcloud

1.0-SNAPSHOT

4.0.0

cloud-consumer-order80

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-actuator

org.springframework.boot

spring-boot-devtools

runtime

true

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

test

3.写yml


server:

port: 80

4.主启动


和之前无区别,自己练习写。名字:OrderMain80

5.业务类


5.1entities

直接复制payment8001

5.2controller

由于不需访问数据库,无service和dao层,更不用写mapper。用户下单后只需要跳转到支付页面,由payment去处理数据库,但是我们没有前端页面,就无法用@controller去找xxx.html,所以怎么处理背后的网页跳转是现在的问题,下面就介绍一个新的类 RestTemplate

RestTemplate是什么?

RestTemplate提供了多种便捷访问远程Http服务的方法,且符合restful,例如 GET 请求、POST 请求、PUT 请求、DELETE 请求以及一些通用的请求执行方法 exchange 以及 execute,能够大大提高客户端的编写效率。

怎么用?

//请求地址

String url = “http://localhost:8080/testPost”;

//入参

RequestBean requestBean = new RequestBean();

requestBean.setTest1(“1”);

requestBean.

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

setTest2(“2”);

requestBean.setTest3(“3”);

RestTemplate restTemplate = new RestTemplate();

ResponseBean responseBean = restTemplate.postForObject(url, requestBean, ResponseBean.class);

使用restTemplate访问restful接口非常的简单粗暴无脑(url, requestMap, ResponseBean.class)这三个参数分别代表 请求地址、请求参数、HTTP响应转换被转换成的对象类型。

RestTemplate方法的名称遵循命名约定,第一部分指出正在调用什么HTTP方法,第二部分指示返回的内容。本例中调用了restTemplate.postForObject方法,post指调用了HTTP的post方法,Object指将HTTP响应转换为您选择的对象类型。还有其他很多类似的方法,可以参考官方api

在本项目的实际运用

1.新建配置类 ApplicationContextConfig 在config包下

package com.li.springcloud.config;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.client.RestTemplate;

@Configuration

public class ApplicationContextConfig {

@Bean

public RestTemplate getRestTemplate(){

return new RestTemplate();

}

}

2.controller调用

package com.li.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;

import com.atguigu.springcloud.entities.Payment;

import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController

@Slf4j

public class OrderController {

public static final String PAYMENT_URL = “http://localhost:8001”;

@Resource

private RestTemplate restTemplate;

@GetMapping("/consumer/payment/create")

public CommonResult create(Payment payment){

return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class); //写操作

}

@GetMapping("/consumer/payment/get/{id}")

public CommonResult getPayment(@PathVariable(“id”) Long id){

return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);

}

}

6.测试


热部署

===

为了调试方便,在开发环境会开启热部署,生产环境会关掉

1.增加依赖和maven插件


要分开添加

org.springframework.boot

spring-boot-devtools

runtime

true

org.springframework.boot

spring-boot-maven-plugin

true

true

2.开启idea自动编译


crtl+shirt+alt+/  选第一个

工程重构

=====

将两个moudle都用到的实体类单独拿出来,通过添加依赖的方式引用

1.新建cloud-api-commons


2.pom


<?xml version="1.0" encoding="UTF-8"?>

<project xmlns=“http://maven.apache.org/POM/4.0.0”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>

cloud2020

com.li.springcloud

1.0-SNAPSHOT

4.0.0

cloud-api-commons

org.springframework.boot

spring-boot-devtools

runtime

true

org.projectlombok

lombok

true

cn.hutool

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值