成都创新互联网站制作重庆分公司

详解SpringBoot通过restTemplate实现消费服务

一、RestTemplate说明

RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。前面的博客中https://www.jb51.net/article/132885.htm,已经使用Jersey客户端来实现了消费spring boot的Restful服务,接下来,我们使用RestTemplate来消费前面示例中的Restful服务,前面的示例:
springboot整合H2内存数据库,实现单元测试与数据库无关性

创新互联自2013年起,是专业互联网技术服务公司,拥有项目成都网站建设、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元奉节做网站,已为上家服务,为奉节各地企业和个人服务,联系电话:13518219792

该示例提供的Restful服务如下:http://localhost:7900/user/1 

{"id":1,"username":"user1","name":"张三","age":20,"balance":100.00} 

二、创建工程

详解SpringBoot通过restTemplate实现消费服务

三、工程结构

详解SpringBoot通过restTemplate实现消费服务

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 
       
     
   
 

四、加入vo

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

package com.chhliu.springboot.restful.vo; 
 
import java.math.BigDecimal; 
 
public class User { 
 private Long id; 
 
 private String username; 
 
 private String name; 
 
 private Short age; 
 
 private BigDecimal balance; 
 
 // ……省略getter和setter方法 
/** 
 * attention: 
 * Details:TODO 
 * @author chhliu 
 * 创建时间:2017-1-20 下午2:05:45 
 * @return 
 */ 
@Override 
public String toString() { 
  return "User [id=" + id + ", username=" + username + ", name=" + name 
      + ", age=" + age + ", balance=" + balance + "]"; 
} 
} 

五,编写controller

package com.chhliu.springboot.restful.controller; 
 
import org.springframework.beans.factory.annotation.Autowired; 
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 com.chhliu.springboot.restful.vo.User; 
 
@RestController 
public class RestTemplateController { 
  @Autowired 
  private RestTemplate restTemplate; 
 
  @GetMapping("/template/{id}") 
  public User findById(@PathVariable Long id) { 
        // http://localhost:7900/user/是前面服务的对应的url 
        User u = this.restTemplate.getForObject("http://localhost:7900/user/" + id, 
        User.class); 
    System.out.println(u); 
    return u; 
  } 
} 

六、启动程序

package com.chhliu.springboot.restful; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.web.client.RestTemplateBuilder; 
import org.springframework.context.annotation.Bean; 
import org.springframework.web.client.RestTemplate; 
 
@SpringBootApplication 
public class SpringbootRestTemplateApplication { 
  // 启动的时候要注意,由于我们在controller中注入了RestTemplate,所以启动的时候需要实例化该类的一个实例 
  @Autowired 
  private RestTemplateBuilder builder; 
 
    // 使用RestTemplateBuilder来实例化RestTemplate对象,spring默认已经注入了RestTemplateBuilder实例 
  @Bean 
  public RestTemplate restTemplate() { 
    return builder.build(); 
  } 
 
  public static void main(String[] args) { 
    SpringApplication.run(SpringbootRestTemplateApplication.class, args); 
  } 
} 

七、测试

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

测试结果如下:

控制台打印结果:

User [id=1, username=user1, name=张三, age=20, balance=100.00] 

通过上面的测试,说明我们已经成功的调用了spring boot的Restful服务。

八、改进

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

User u = this.restTemplate.getForObject("http://localhost:7900/user/" + id, 
        User.class); 

此处出现了硬编码,当服务器地址改变的时候,需要改动对应的代码,改进的方法,将Restful服务的地址写到配置文件中。
修改controller如下:

package com.chhliu.springboot.restful.controller; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Value; 
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 com.chhliu.springboot.restful.vo.User; 
 
@RestController 
public class RestTemplateController { 
  @Autowired 
  private RestTemplate restTemplate; 
   
    // Restful服务对应的url地址 
  @Value("${user.userServicePath}") 
  private String userServicePath; 
 
  @GetMapping("/template/{id}") 
  public User findById(@PathVariable Long id) { 
    User u = this.restTemplate.getForObject(this.userServicePath + id, User.class); 
    System.out.println(u); 
    return u; 
  } 
} 

配置文件修改如下:

server.port:7902 
user.userServicePath=http://localhost:7900/user/ 

启动程序:

发现测试是ok的,后面我们会引入spring cloud对这种调用方式进行进一步的改进!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


当前标题:详解SpringBoot通过restTemplate实现消费服务
标题网址:http://cxhlcq.com/article/jchjgj.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部