当前位置: 移动技术网 > IT编程>开发语言>Java > 详解SpringBoot通过restTemplate实现消费服务

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

一、resttemplate说明

resttemplate是spring提供的用于访问rest服务的客户端,resttemplate提供了多种便捷访问远程http服务的方法,能够大大提高客户端的编写效率。前面的博客中,已经使用jersey客户端来实现了消费spring boot的restful服务,接下来,我们使用resttemplate来消费前面示例中的restful服务,前面的示例:

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

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

二、创建工程

三、工程结构

pom文件依赖如下:

<?xml version="1.0" encoding="utf-8"?> 
<project 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"> 
  <modelversion>4.0.0</modelversion> 
 
  <groupid>com.chhliu.springboot.restful</groupid> 
  <artifactid>springboot-rest-template</artifactid> 
  <version>0.0.1-snapshot</version> 
  <packaging>jar</packaging> 
 
  <name>springboot-rest-template</name> 
  <description>demo project for spring boot resttemplate</description> 
 
  <parent> 
    <groupid>org.springframework.boot</groupid> 
    <artifactid>spring-boot-starter-parent</artifactid> 
    <version>1.4.3.release</version> 
    <relativepath/> <!-- lookup parent from repository --> 
  </parent> 
 
  <properties> 
    <project.build.sourceencoding>utf-8</project.build.sourceencoding> 
    <project.reporting.outputencoding>utf-8</project.reporting.outputencoding> 
    <java.version>1.7</java.version> 
  </properties> 
 
  <dependencies> 
    <dependency> 
      <groupid>org.springframework.boot</groupid> 
      <artifactid>spring-boot-starter-web</artifactid> 
    </dependency> 
 
    <dependency> 
      <groupid>org.springframework.boot</groupid> 
      <artifactid>spring-boot-starter-test</artifactid> 
      <scope>test</scope> 
    </dependency> 
    <!-- 热启动,热部署依赖包,为了调试方便,加入此包 --> 
    <dependency> 
      <groupid>org.springframework.boot</groupid> 
      <artifactid>spring-boot-devtools</artifactid> 
      <optional>true</optional> 
    </dependency> 
  </dependencies> 
 
  <build> 
    <plugins> 
      <plugin> 
        <groupid>org.springframework.boot</groupid> 
        <artifactid>spring-boot-maven-plugin</artifactid> 
      </plugin> 
    </plugins> 
  </build> 
</project> 

四、加入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对这种调用方式进行进一步的改进!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网