RestTemplate - 使用


將多個服務注冊到 Eureka上,使用RestTemplate進行調用:

演示 APP-RESTTEMPLATE 通過 RestTemplate調用 API-02-APPLICATION

應用 API-02-APPLICATION 配置

准備

配置文件

server:
  port: 8001
eureka:
  client:
    # 設置服務注冊中心的url
    service-url:
      defaultZone: http://localhost:7900/eureka
  instance:
    instance-id: qiankai-client-002 #顯示此名字(默認是當前項目http://localhost:8001)
    prefer-ip-address: true #訪問路徑可以顯示ip地址

spring:
  application:
    name: api-02-application

配置

Controller如下:

package com.qiankai.api.controller;

import com.qiankai.common.dto.Person;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

/**
 * 演示RestTemplate
 * 給其他客戶端調用
 *
 * @author kai_qian
 * @version v1.0
 * @since 2020/09/15 16:32
 */
@RestController
public class RestTemplateApiController {

    @GetMapping("/getMap")
    public Map<String, String> getMap() {
        HashMap<String, String> map = new HashMap<>();
        map.put("name", "500");
        return map;
    }

    @GetMapping("/getObj")
    public Person getObj() {
        Person person = new Person();
        person.setId(100);
        person.setName("xiaoming");
        return person;
    }

    @GetMapping("/getObjParam")
    public Person getObjParam(String name) {
        Person person = new Person();
        person.setId(100);
        person.setName(name);
        return person;
    }

    @PostMapping("/postParam")
    public Person postParam(@RequestBody String name) {
        System.out.println("name:" + name);
        Person person = new Person();
        person.setId(100);
        person.setName("xiaoming" + name);
        return person;
    }

    @PostMapping("/postForLocation")
    public URI postParam(@RequestBody Person person, HttpServletResponse response) throws Exception {

        URI uri = new URI("https://www.baidu.com/s?wd=" + person.getName());
        response.addHeader("Location", uri.toString());
        return uri;
    }
}

應用 APP-RESTTEMPLATE 配置

准備

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">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>client-resttemplate</artifactId>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.qiankai</groupId>
            <artifactId>client-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 用於上報節點信息 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

配置文件

server:
  port: 8010
eureka:
  client:
    # 設置服務注冊中心的url
    service-url:
      defaultZone: http://localhost:7900/eureka
  instance:
    instance-id: qiankai-resttemplate #顯示此名字(默認是當前項目http://localhost:8001)
    prefer-ip-address: true #訪問路徑可以顯示ip地址

spring:
  application:
    name: app-restTemplate

配置

先配置一個 RestTemplate實例:

package com.qiankai.rest.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * 配置
 *
 * @author kai_qian
 * @version v1.0
 * @since 2020/09/15 16:41
 */
@Configuration
public class AppConfig {

    @Bean
    @LoadBalanced // 開啟負載均衡
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

Controller 如下:

package com.qiankai.rest.controller;

import com.qiankai.common.dto.Person;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.Collections;
import java.util.Map;

/**
 * 使用RestTemplate調用其他客戶端接口
 *
 * @author kai_qian
 * @version v1.0
 * @since 2020/09/15 16:42
 */
@RestController
public class CallApiController {

    // 地址使用配置的應用名(Eureka中顯示的)
    private static final String CLIENT002_8001_URL = "http://API-02-APPLICATION"; 

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 返回Map
     */
    @GetMapping("/getMap")
    public void getMap() {
        String url = CLIENT002_8001_URL + "/getMap";
        ResponseEntity<Map> entity = restTemplate.getForEntity(url, Map.class);
        System.out.println("getMap返回: " + entity.getBody());
    }

    /**
     * 返回對象
     */
    @GetMapping("/getObj")
    public void getObj() {
        String url = CLIENT002_8001_URL + "/getObj";
        ResponseEntity<Person> entity = restTemplate.getForEntity(url, Person.class);
        System.out.println("getObj返回: " + ToStringBuilder.reflectionToString(entity.getBody()));
    }

    /**
     * 傳參調用
     */
    @GetMapping("/getObjParam")
    public void getObjParams() {
        // 使用占位符
        String url1 = CLIENT002_8001_URL + "/getObjParam?name={1}";
        ResponseEntity<Person> entity1 = restTemplate.getForEntity(url1, Person.class, "hehehe...");
        System.out.println("getObjParam返回: " + entity1.getBody());
        // 使用map
        String url2 = CLIENT002_8001_URL + "/getObjParam?name={name}";
        Map<String, String> map = Collections.singletonMap("name", " memeda");
        ResponseEntity<Person> entity2 = restTemplate.getForEntity(url2, Person.class, map);
        System.out.println("getObjParam返回: " + entity2.getBody());

        // 返回對象
        String url3 = CLIENT002_8001_URL + "/getObjParam?name=tatata";
        Person person = restTemplate.getForObject(url3, Person.class, map);
        System.out.println("getObjParam返回: " + person.toString());
    }

    /**
     * post請求
     */
    @GetMapping("/postParam")
    public void postParams() {
        String url = CLIENT002_8001_URL + "/postParam";
        Map<String, String> map = Collections.singletonMap("name", " memeda");
        ResponseEntity<Person> entity = restTemplate.postForEntity(url, map, Person.class);
        System.out.println("postParam返回:" + entity.getBody());
    }

    /**
     * postForLocation
     */
    @GetMapping("/postForLocation")
    public void postForLocation() {
        String url = CLIENT002_8001_URL + "/postForLocation";
        Map<String, String> map = Collections.singletonMap("name", "memeda");
        URI location = restTemplate.postForLocation(url, map, Person.class);
        System.out.println("postForLocation返回:" + location);
    }
}

演示結果

訪問 APP-RESTTEMPLATE 應用的接口,通過 RestTemplate調用的信息被打印在控制台:

使用攔截器

添加一個類,實現 ClientHttpRequestInterceptor接口

package com.qiankai.rest.config;

import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;

/**
 * 客戶端調用攔截器配置
 *
 * @author kai_qian
 * @version v1.0
 * @since 2020/09/15 17:46
 */
public class LoggingClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        System.out.println("進入攔截器了");
        System.out.println("請求的URL是:"+request.getURI());
        ClientHttpResponse response = execution.execute(request, body);
        System.out.println("打印響應頭:"+response.getHeaders());
        return response;
    }
}

修改 RestTemplate配置:

package com.qiankai.rest.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * 配置
 *
 * @author kai_qian
 * @version v1.0
 * @since 2020/09/15 16:41
 */
@Configuration
public class AppConfig {

    @Bean
    @LoadBalanced // 開啟負載均衡
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        // 添加一個攔截器
        restTemplate.getInterceptors().add(new LoggingClientHttpRequestInterceptor());
        return restTemplate;
    }
}

調用 /postParam 接口,控制台打印日志,說明攔截器生效了:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM