分享

使用RestTemplate消费spring boot的Restful服务

 观天落子 2018-06-07
RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。前面的博客中http://blog.csdn.net/liuchuanhong1/article/details/53537874,已经使用Jersey客户端来实现了消费spring boot的Restful服务,接下来,我们使用RestTemplate来消费前面示例中的Restful服务,前面的示例:

springboot整合H2内存数据库,实现单元测试与数据库无关性

该示例提供的Restful服务如下:
  1. http://localhost:7900/user/1  
  1. {"id":1,"username":"user1","name":"张三","age":20,"balance":100.00}  

二、创建工程

三、工程结构

pom文件依赖如下:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven./POM/4.0.0" xmlns:xsi="http://www./2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://maven./POM/4.0.0 http://maven./xsd/maven-4.0.0.xsd">  
  4.     <modelVersion>4.0.0</modelVersion>  
  5.   
  6.     <groupId>com.chhliu.springboot.restful</groupId>  
  7.     <artifactId>springboot-rest-template</artifactId>  
  8.     <version>0.0.1-SNAPSHOT</version>  
  9.     <packaging>jar</packaging>  
  10.   
  11.     <name>springboot-rest-template</name>  
  12.     <description>Demo project for Spring Boot RestTemplate</description>  
  13.   
  14.     <parent>  
  15.         <groupId>org.springframework.boot</groupId>  
  16.         <artifactId>spring-boot-starter-parent</artifactId>  
  17.         <version>1.4.3.RELEASE</version>  
  18.         <relativePath/> <!-- lookup parent from repository -->  
  19.     </parent>  
  20.   
  21.     <properties>  
  22.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  23.         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
  24.         <java.version>1.7</java.version>  
  25.     </properties>  
  26.   
  27.     <dependencies>  
  28.         <dependency>  
  29.             <groupId>org.springframework.boot</groupId>  
  30.             <artifactId>spring-boot-starter-web</artifactId>  
  31.         </dependency>  
  32.   
  33.         <dependency>  
  34.             <groupId>org.springframework.boot</groupId>  
  35.             <artifactId>spring-boot-starter-test</artifactId>  
  36.             <scope>test</scope>  
  37.         </dependency>  
  38.         <!-- 热启动,热部署依赖包,为了调试方便,加入此包 -->  
  39.         <dependency>  
  40.             <groupId>org.springframework.boot</groupId>  
  41.             <artifactId>spring-boot-devtools</artifactId>  
  42.             <optional>true</optional>  
  43.         </dependency>  
  44.     </dependencies>  
  45.   
  46.     <build>  
  47.         <plugins>  
  48.             <plugin>  
  49.                 <groupId>org.springframework.boot</groupId>  
  50.                 <artifactId>spring-boot-maven-plugin</artifactId>  
  51.             </plugin>  
  52.         </plugins>  
  53.     </build>  
  54. </project>  
四、加入vo

由于我们使用RestTemplate调用Restful服务后,需要将对应的json串转换成User对象,所以需要将这个类拷贝到该工程中,如下:

  1. package com.chhliu.springboot.restful.vo;  
  2.   
  3. import java.math.BigDecimal;  
  4.   
  5. public class User {  
  6.   private Long id;  
  7.   
  8.   private String username;  
  9.   
  10.   private String name;  
  11.   
  12.   private Short age;  
  13.   
  14.   private BigDecimal balance;  
  15.   
  16.   // ……省略getter和setter方法  
  17. /** 
  18.  * attention: 
  19.  * Details:TODO 
  20.  * @author chhliu 
  21.  * 创建时间:2017-1-20 下午2:05:45 
  22.  * @return 
  23.  */  
  24. @Override  
  25. public String toString() {  
  26.     return "User [id=" + id + ", username=" + username + ", name=" + name  
  27.             + ", age=" + age + ", balance=" + balance + "]";  
  28. }  
  29. }  
五,编写controller
  1. package com.chhliu.springboot.restful.controller;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.web.bind.annotation.GetMapping;  
  5. import org.springframework.web.bind.annotation.PathVariable;  
  6. import org.springframework.web.bind.annotation.RestController;  
  7. import org.springframework.web.client.RestTemplate;  
  8.   
  9. import com.chhliu.springboot.restful.vo.User;  
  10.   
  11. @RestController  
  12. public class RestTemplateController {  
  13.     @Autowired  
  14.     private RestTemplate restTemplate;  
  15.   
  16.     @GetMapping("/template/{id}")  
  17.     public User findById(@PathVariable Long id) {  
  18.                // http://localhost:7900/user/是前面服务的对应的url  
  19.                User u = this.restTemplate.getForObject("http://localhost:7900/user/" + id,  
  20.                 User.class);  
  21.         System.out.println(u);  
  22.         return u;  
  23.     }  
  24. }  
六、启动程序
  1. package com.chhliu.springboot.restful;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.boot.SpringApplication;  
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  6. import org.springframework.boot.web.client.RestTemplateBuilder;  
  7. import org.springframework.context.annotation.Bean;  
  8. import org.springframework.web.client.RestTemplate;  
  9.   
  10. @SpringBootApplication  
  11. public class SpringbootRestTemplateApplication {  
  12.     // 启动的时候要注意,由于我们在controller中注入了RestTemplate,所以启动的时候需要实例化该类的一个实例  
  13.     @Autowired  
  14.     private RestTemplateBuilder builder;  
  15.   
  16.         // 使用RestTemplateBuilder来实例化RestTemplate对象,spring默认已经注入了RestTemplateBuilder实例  
  17.     @Bean  
  18.     public RestTemplate restTemplate() {  
  19.         return builder.build();  
  20.     }  
  21.   
  22.     public static void main(String[] args) {  
  23.         SpringApplication.run(SpringbootRestTemplateApplication.class, args);  
  24.     }  
  25. }  
七、测试

在浏览器中输入:http://localhost:7902/template/1

测试结果如下:

控制台打印结果:

  1. User [id=1, username=user1, name=张三, age=20, balance=100.00]  
通过上面的测试,说明我们已经成功的调用了spring boot的Restful服务。

八、改进

上面的测试中,有一个很不好的地方,

  1. User u = this.restTemplate.getForObject("http://localhost:7900/user/" + id,  
  2.                 User.class);  
此处出现了硬编码,当服务器地址改变的时候,需要改动对应的代码,改进的方法,将Restful服务的地址写到配置文件中。

修改controller如下:

  1. package com.chhliu.springboot.restful.controller;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.beans.factory.annotation.Value;  
  5. import org.springframework.web.bind.annotation.GetMapping;  
  6. import org.springframework.web.bind.annotation.PathVariable;  
  7. import org.springframework.web.bind.annotation.RestController;  
  8. import org.springframework.web.client.RestTemplate;  
  9.   
  10. import com.chhliu.springboot.restful.vo.User;  
  11.   
  12. @RestController  
  13. public class RestTemplateController {  
  14.     @Autowired  
  15.     private RestTemplate restTemplate;  
  16.       
  17.         // Restful服务对应的url地址  
  18.     @Value("${user.userServicePath}")  
  19.     private String userServicePath;  
  20.   
  21.     @GetMapping("/template/{id}")  
  22.     public User findById(@PathVariable Long id) {  
  23.         User u = this.restTemplate.getForObject(this.userServicePath + id, User.class);  
  24.         System.out.println(u);  
  25.         return u;  
  26.     }  
  27. }  
配置文件修改如下:
  1. server.port:7902  
  2. user.userServicePath=http://localhost:7900/user/  
启动程序: 发现测试是ok的,后面我们会引入spring cloud对这种调用方式进行进一步的改进!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多