国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - 詳解SpringBoot通過(guò)restTemplate實(shí)現(xiàn)消費(fèi)服務(wù)

詳解SpringBoot通過(guò)restTemplate實(shí)現(xiàn)消費(fèi)服務(wù)

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

本篇文章主要介紹了詳解使用RestTemplate消費(fèi)spring boot的Restful服務(wù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

 

一、RestTemplate說(shuō)明

 

RestTemplate是Spring提供的用于訪問(wèn)Rest服務(wù)的客戶端,RestTemplate提供了多種便捷訪問(wèn)遠(yuǎn)程Http服務(wù)的方法,能夠大大提高客戶端的編寫(xiě)效率。前面的博客中http://www.jfrwli.cn/article/149367.html,已經(jīng)使用Jersey客戶端來(lái)實(shí)現(xiàn)了消費(fèi)spring boot的Restful服務(wù),接下來(lái),我們使用RestTemplate來(lái)消費(fèi)前面示例中的Restful服務(wù),前面的示例:
springboot整合H2內(nèi)存數(shù)據(jù)庫(kù),實(shí)現(xiàn)單元測(cè)試與數(shù)據(jù)庫(kù)無(wú)關(guān)性

該示例提供的Restful服務(wù)如下:http://localhost:7900/user/1 

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

 

二、創(chuàng)建工程

 

詳解SpringBoot通過(guò)restTemplate實(shí)現(xiàn)消費(fèi)服務(wù)

 

三、工程結(jié)構(gòu)

 

詳解SpringBoot通過(guò)restTemplate實(shí)現(xiàn)消費(fèi)服務(wù)

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>
    <!-- 熱啟動(dòng),熱部署依賴包,為了調(diào)試方便,加入此包 -->
    <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調(diào)用Restful服務(wù)后,需要將對(duì)應(yīng)的json串轉(zhuǎn)換成User對(duì)象,所以需要將這個(gè)類拷貝到該工程中,如下:

?
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
 * 創(chuàng)建時(shí)間:2017-1-20 下午2:05:45
 * @return
 */
@Override
public String toString() {
  return "User [id=" + id + ", username=" + username + ", name=" + name
      + ", age=" + age + ", balance=" + balance + "]";
}
}

 

五,編寫(xiě)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/是前面服務(wù)的對(duì)應(yīng)的url
        User u = this.restTemplate.getForObject("http://localhost:7900/user/" + id,
        User.class);
    System.out.println(u);
    return u;
  }
}

 

六、啟動(dòng)程序

 

?
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 {
  // 啟動(dòng)的時(shí)候要注意,由于我們?cè)赾ontroller中注入了RestTemplate,所以啟動(dòng)的時(shí)候需要實(shí)例化該類的一個(gè)實(shí)例
  @Autowired
  private RestTemplateBuilder builder;
 
    // 使用RestTemplateBuilder來(lái)實(shí)例化RestTemplate對(duì)象,spring默認(rèn)已經(jīng)注入了RestTemplateBuilder實(shí)例
  @Bean
  public RestTemplate restTemplate() {
    return builder.build();
  }
 
  public static void main(String[] args) {
    SpringApplication.run(SpringbootRestTemplateApplication.class, args);
  }
}

 

七、測(cè)試

 

在瀏覽器中輸入:http://localhost:7902/template/1

測(cè)試結(jié)果如下:

控制臺(tái)打印結(jié)果:

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

通過(guò)上面的測(cè)試,說(shuō)明我們已經(jīng)成功的調(diào)用了spring boot的Restful服務(wù)。

 

八、改進(jìn)

 

上面的測(cè)試中,有一個(gè)很不好的地方,

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

此處出現(xiàn)了硬編碼,當(dāng)服務(wù)器地址改變的時(shí)候,需要改動(dòng)對(duì)應(yīng)的代碼,改進(jìn)的方法,將Restful服務(wù)的地址寫(xiě)到配置文件中。
修改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服務(wù)對(duì)應(yīng)的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/

啟動(dòng)程序:

發(fā)現(xiàn)測(cè)試是ok的,后面我們會(huì)引入spring cloud對(duì)這種調(diào)用方式進(jìn)行進(jìn)一步的改進(jìn)!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 糈精国产xxxx在线观看 | 91精品一区二区 | 欧美做爰一区二区三区 | 久久久久国产 | 精品成人免费一区二区在线播放 | 免费一级片视频 | 午夜精品久久久久久久久久久久久 | 亚洲一区二区三区免费观看 | 成人免费黄色毛片 | 日韩精品免费在线视频 | 激情国产视频 | 久久香蕉国产 | 久久国产精品一区 | 国产aaaaav久久久一区二区 | 日韩成人av电影在线观看 | 成人久久久 | 美女视频黄色片 | 亚洲成人一区二区三区 | 91精品国产色综合久久 | 日本精品在线观看视频 | av在线视| 在线亚洲精品 | av在线播放网 | 国产一区二区三区久久 | 成人午夜精品久久久久久久蜜臀 | 成人午夜性a一级毛片免费看 | 毛片免费在线 | 亚洲福利一区 | 亚洲一区二区 | 毛片一卡 | 日韩影院在线 | 亚洲欧美视频在线播放 | 欧美黑人一级爽快片淫片高清 | 在线观看一区二区三区视频 | 少妇一区二区三区免费观看 | 免费亚洲婷婷 | 亚洲狠狠丁香婷婷综合久久久 | 夜夜春精品视频高清69式 | 一级录像免费录像在线观看 | 国产欧美日韩综合精品一区二区 | 激情网页 |