
RestTemplate是Spring提供的用于訪問Rest服務(wù)的客戶端,RestTemplate提供了多種便捷訪問遠(yuǎn)程Http服務(wù)的方法,能夠大大提高客戶端的編寫效率。前面的博客中https://www.jb51.net/article/132885.htm,已經(jīng)使用Jersey客戶端來實(shí)現(xiàn)了消費(fèi)spring boot的Restful服務(wù),接下來,我們使用RestTemplate來消費(fèi)前面示例中的Restful服務(wù),前面的示例:
springboot整合H2內(nèi)存數(shù)據(jù)庫,實(shí)現(xiàn)單元測試與數(shù)據(jù)庫無關(guān)性

該示例提供的Restful服務(wù)如下:http://localhost:7900/user/1
{"id":1,"username":"user1","name":"張三","age":20,"balance":100.00}
pom文件依賴如下:
<?xml version="1.0" encoding="UTF-8"?>4.0.0 com.chhliu.springboot.restful springboot-rest-template 0.0.1-SNAPSHOT jar springboot-rest-template Demo project for Spring Boot RestTemplate org.springframework.boot spring-boot-starter-parent 1.4.3.RELEASE UTF-8 UTF-8 1.7 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-devtools true org.springframework.boot spring-boot-maven-plugin