RestTemplate

yapi

swagger

服务间调用RestTemplate

 

1. 利用RestTemplate实现负载均衡

在配置RestTemplate上加@LoadBalanced 注解

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return  new RestTemplate();
    }

不需要在去choose provider服务,之间用服务id就可以了。就会自动实现负载均衡。 

    @RequestMapping("/K")
    public String getK(){

        String url = "http://provider/hello";
        String forObject = restTemplate.getForObject(url, String.class);
        return forObject;
    }

 

 

2.  Get请求

 

//参数为Spring,返回为spring
    @RequestMapping("/L")
    public String getL(){
        String url = "http://provider/hello2?name={1}";
        String forObject = restTemplate.getForObject(url,String.class,"zhangsan");
        return forObject;
    }

 

 

//参数为Map,返回为spring
    @RequestMapping("/M")
    public String getM(){

        String url = "http://provider/hello3?name={name}";
        Map<String, String> stringStringMap = Collections.singletonMap("name", "lisi");
        String forObject = restTemplate.getForObject(url, String.class, stringStringMap);
        return forObject;
    }

 

 

//参数为map,返回为map
    @RequestMapping("/O")
    public String getO(){

        String url = "http://provider/hello4?name={name}";
        Map<String, String> stringStringMap = Collections.singletonMap("name", "lisi");
        Map<String,Object> forObject = restTemplate.getForObject(url, Map.class, stringStringMap);
        return forObject.toString();
    }

 

3. Post请求

发送post请求,

//Post请求,参数为对象,返回为String
    @GetMapping("/N")
    public String save(){
        String url = "http://provider/hello5";
        Map<String, String> stringStringMap = Collections.singletonMap("name", "lisi");
        Person person = new Person("东方不败",22);
        String s = restTemplate.postForObject(url, person, String.class);
        return s;
    }

 

 处理post请求

@RequestMapping(value = "/hello5",method = RequestMethod.POST)
    public String getHello5(@RequestBody Map name) {
        System.out.println(name);
        return "收到了:"+name;
    }

    @PostMapping(value = "/hello6")
    public String getHello6(@RequestBody String name) {
        System.out.println(name);
        return "收到了:"+name;
    }

 

4. 拦截器

需要实现ClientHttpRequestInterceptor接口

package com.example.interceptor;

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;

/**
 * 拦截器
 */
public class MyInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
        System.out.println("01请求进入拦截......");
        System.out.println("进入查看头信息:"+httpRequest.getHeaders());
        System.out.println("02 开始执行请求---");
        ClientHttpResponse clientHttpResponse = clientHttpRequestExecution.execute(httpRequest, bytes);
        System.out.println("03 返回响应---");

        System.out.println("响应查看头信息:"+clientHttpResponse.getHeaders());

        return clientHttpResponse;
    }
}

将拦截器添加到RestTemplate中

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getInterceptors().add(new MyInterceptor());
        return  restTemplate;
    }

RestTemplate调用请求

//Post请求,参数为对象,返回为String
    @GetMapping("/N")
    public String save(){
        System.out.println("------------------");
        String url = "http://provider/hello5";
        Map<String, String> stringStringMap = Collections.singletonMap("name", "lisi");
        Person person = new Person("东方不败",22);
        String s = restTemplate.postForObject(url, person, String.class);
        System.out.println("------------------");
        return s;
    }

拦截的地方,就是在RestTemplate调用方法的前后,在发请求之前进行拦截,调用方法后,返回之前再次拦截。

 

 

5.  RestTemplate的其他方法

restTemplate.exchange()可以自定义http请求的头信息,同时保护get和post方法 

restTemplate.getForEntity(),跟getForObject() 方法类型,只是返回了多了一些头信息等。

postForLocation() 方法

调用方

		String url ="http://provider/postParam";
		   
		Map<String, String> map = Collections.singletonMap("name", " memeda");
		URI location = restTemplate.postForLocation(url, map, Person.class);
		
		System.out.println(location);

提供方,需要设置头信息,不然返回的是null

	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());

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值