<强>一、创建RestTemplate说明强>
创建RestTemplate是春天提供的用于访问其他服务的客户端,创建RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。前面的博客中https://www.jb51.net/article/132885.htm,已经使用球衣客户端来实现了消费弹簧引导的Restful服务,接下来,我们使用创建RestTemplate来消费前面示例中的Restful服务,前面的示例:
springboot整合H2内存数据库,实现单元测试与数据库无关性
该示例提供的Restful服务如下:http://localhost: 7900/user/1,
{" id ": 1、“用户名”:“user1”、“名称”:“张三”,“年龄”:20,“平衡”:100.00},
二、创建工程
三、工程结构
pom文件依赖如下:
& lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比; & lt;项目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”比; & lt; modelVersion> 4.0.0 & lt; groupId> com.chhliu.springboot.restful & lt; artifactId> springboot-rest-template & lt; version> 0.0.1-SNAPSHOT & lt; packaging> jar & lt; name> springboot-rest-template 春天& lt; description>演示项目引导RestTemplate & lt; parent> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-parent & lt; version> 1.4.3.RELEASE & lt; relativePath/比;& lt; !——从库中查找父——比; & lt;/parent> & lt; properties> & lt; project.build.sourceEncoding> UTF-8 & lt; project.reporting.outputEncoding> UTF-8 & lt; java.version> 1.7 & lt;/java.version> & lt;/properties> & lt; dependencies> & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-web & lt;/dependency> & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-test & lt; scope> test & lt;/dependency> & lt; !——热启动,热部署依赖包,为了调试方便,加入此包——比; & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-devtools & lt; optional> true & lt;/dependency> & lt;/dependencies> & lt; build> & lt; plugins> & lt; plugin> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-maven-plugin & lt;/plugin> & lt;/plugins> & lt;/build> & lt;/project> >之前四,加入vo
由于我们使用创建RestTemplate调用Restful服务后,需要将对应的json串转换成用户对象,所以需要将这个类拷贝到该工程中,如下:
包com.chhliu.springboot.restful.vo; 进口java.math.BigDecimal; 公开课用户{ 私人长id; 私人字符串的用户名; 私人字符串名称; 私人短年龄; 私人BigDecimal平衡;//÷詆etter和setter方法/* * *注意: *细节:待办事项 * @author chhliu *创建时间:2017-1-20下午2:05:45 * @return */@Override 公共字符串toString () { 返回“用户id=" +身份证+”,用户名=" +用户名+”,name=" +名字 +”、年龄=" +年龄+”,平衡=" +平衡+“]”; } } >之前五、编写控制器/h3>
包com.chhliu.springboot.restful.controller; 进口org.springframework.beans.factory.annotation.Autowired; 进口org.springframework.web.bind.annotation.GetMapping; 进口org.springframework.web.bind.annotation.PathVariable; 进口org.springframework.web.bind.annotation.RestController; 进口org.springframework.web.client.RestTemplate; 进口com.chhliu.springboot.restful.vo.User; @RestController 公开课RestTemplateController { @ autowired 私人创建RestTemplate创建RestTemplate; @GetMapping(“/模板/{id}”) 公共用户findById (@PathVariable长id) {//http://localhost: 7900/user/是前面服务的对应的url 用户u=this.restTemplate。getForObject (http://localhost: 7900/user/+身份证, User.class); System.out.println (u); 回报你; } }详解SpringBoot通过创建restTemplate实现消费服务