服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 详解SpringBoot通过restTemplate实现消费服务

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

2021-03-22 13:34牛奋lch Java教程

本篇文章主要介绍了详解使用RestTemplate消费spring boot的Restful服务,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

 

一、RestTemplate说明

 

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

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

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

 

二、创建工程

 

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

 

三、工程结构

 

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

pom文件依赖如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?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对象,所以需要将这个类拷贝到该工程中,如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
  }
}

 

六、启动程序

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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服务。

 

八、改进

 

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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;
  }
}

配置文件修改如下:

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

启动程序:

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/liuchuanhong1/article/details/54631080

延伸 · 阅读

精彩推荐