SpringCloud之RestTemplate的使用-創(chuàng)新互聯(lián)

源碼

10余年專注成都網(wǎng)站制作,企業(yè)網(wǎng)站建設(shè),個人網(wǎng)站制作服務(wù),為大家分享網(wǎng)站制作知識、方案,網(wǎng)站設(shè)計流程、步驟,成功服務(wù)上千家企業(yè)。為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計及定制高端網(wǎng)站建設(shè)服務(wù),專注于企業(yè)網(wǎng)站建設(shè),高端網(wǎng)頁制作,對成都銅雕雕塑等多個方面,擁有豐富設(shè)計經(jīng)驗(yàn)。

GitHub

RestTemplate是什么?

RestTemplate是Spring對Http客戶端進(jìn)行封裝的一個模板工具類,對常用的Http客戶端例如:HttpClient、OKHttp、JDK原生的URLConnection(默認(rèn)的)都支持。

RestTemplate能做什么?

基于Restful風(fēng)格可以對遠(yuǎn)程服務(wù)基于http協(xié)議進(jìn)行遠(yuǎn)程調(diào)用

RestTemplate的使用

新建一個父工程,命名為springcloud-eureka01

目錄結(jié)構(gòu)如下圖:

pom.xml:

org.springframework.boot

spring-boot-starter-parent

2.0.0.RELEASE

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-devtools

runtime

true

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-maven-plugin

新建模塊,命名為eureka-provider01

目錄結(jié)構(gòu)如下圖:

pom.xml:

com.wyj

springcloud-eureka01

0.0.1-SNAPSHOT

org.springframework.boot

spring-boot-starter-data-jpa

org.springframework.boot

spring-boot-starter-web

mysql

mysql-connector-java

runtime

org.projectlombok

lombok

true

ProviderController:

@Controller

public class ProviderController {

@Autowired

private UserService userService;

@RequestMapping(value = "/{id}")

@ResponseBody

public User findById(@PathVariable int id) {

User user = userService.findById(id);

return user;

}

}

UserService:

public interface UserService {

public User findById(int id);

}

UserServiceImpl:

@Service

public class UserServiceImpl implements UserService {

@Autowired

private UserRepository userRepository;

@Override

public User findById(int id) {

Optional userOptional = userRepository.findById(id);

User user = userOptional.get();

return user;

}

}

UserRepository:

public interface UserRepository extends JpaRepository {

}

User:

@Entity

@Data

public class User implements Serializable {

@Id

@GeneratedValue

private int id;

private String name;

private String sex;

private int age;

private String address;

private String phone;

}

application.properties:

server.port=8081

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springcloud-eureka01?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.username=root

spring.datasource.password=root

spring.jpa.show-sql=true

spring.jpa.hibernate.ddl-auto=update

spring.jpa.properties.hibernate.format_sql=true

spring.jpa.properties.hibernate.use_sql_comments=true

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

新建模塊,命名為eureka-consumer01

目錄結(jié)構(gòu)如下圖:

pom.xml:

com.wyj

springcloud-eureka01

0.0.1-SNAPSHOT

org.springframework.boot

spring-boot-starter-web

org.projectlombok

lombok

true

ConsumerController:

@Controller

public class ConsumerController{

@Autowired

private RestTemplate restTemplate;

@RequestMapping(value = "/findById/{id}")

@ResponseBody

public User findById(@PathVariable int id) {

User user = restTemplate.getForObject("http://127.0.0.1:8081/" + id, User.class);

return user;

}無錫×××醫(yī)院 https://yyk.familydoctor.com.cn/20612/

}

RestConfig:

@Configuration

public class RestConfig {

@Bean

public RestTemplate restTemplate() {

return new RestTemplate();

}

}

User:

@Data

public class User implements Serializable {

private int id;

private String name;

private String sex;

private int age;

private String address;

private String phone;

}

測試使用

分別啟動eureka-provider01和eureka-consumer01,啟動順序沒關(guān)系

瀏覽器訪問http://127.0.0.1:8080

總結(jié)

在ConsumerController中,RestTemplate調(diào)用遠(yuǎn)程服務(wù)

User user = restTemplate.getForObject("http://127.0.0.1:8081", User.class);

RestTemplate通過getForObject方法傳遞url地址及實(shí)體類的字節(jié)碼,自動發(fā)起請求,接收響應(yīng),并且?guī)臀覀儗憫?yīng)結(jié)果通過SpringMVC內(nèi)置的Jackson中的ObjectMapper進(jìn)行json反序列化,最后返回一個User對象

雖然RestTemplate能夠調(diào)用遠(yuǎn)程服務(wù),但是這樣還存在著很多問題:

1、請求地址http://127.0.0.1:8081/硬編碼了,如果更換環(huán)境,那么需要更改大量的代碼

2、并沒有一個中心來控制并管理provider和consumer之間的關(guān)系與調(diào)用

3、使用RestTemplate調(diào)用遠(yuǎn)程服務(wù)代碼開發(fā)量相對而言大,現(xiàn)在的Demo是業(yè)務(wù)簡單,只需要簡單調(diào)用getForObject就能獲取數(shù)據(jù)。事實(shí)上,RestTemplate還有大量基于Http協(xié)議的方法,比如delete()、getForEntity()、getForObject()、put()、headForHeaders()等。但是在后期的SpringCloud學(xué)習(xí)過程中,F(xiàn)eign作為SpringCloud的核心組件,用于遠(yuǎn)程服務(wù)調(diào)用,也是基于Http協(xié)議,并且支持注解開發(fā),能夠大量減少開發(fā)量

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

文章標(biāo)題:SpringCloud之RestTemplate的使用-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://muchs.cn/article0/eicoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、標(biāo)簽優(yōu)化、定制開發(fā)、服務(wù)器托管、移動網(wǎng)站建設(shè)網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營