controller类org.springframework.boot spring-boot-starter-web
package cn.org.emcs.userbasecloud.test;
import cn.org.emcs.common.vo.Res;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
@RequestMapping("test")
@RestController
@Slf4j
public class TestController {
@GetMapping()
public Res get(String name){
log.info("requestParam is: {}",name);
return Res.success("name is: "+name);
}
@PostMapping
public Res post(@RequestBody Object o){
log.info("requestBody is: {}",o);
return Res.success(o.toString());
}
}
实现类
package com.example.restTemplate;
import com.example.restTemplate.vo.TLayercategoryVO;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.linkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
public class RestTemplateTest01 {
public static void main(String[] args) {
RestTemplate template = new RestTemplate();
String url = "http://192.168.1.21:8081/test?name=123";
// 封装参数,千万不要替换为Map与HashMap,否则参数无法传递
MultiValueMap paramMap = new linkedMultiValueMap();
paramMap.add("layercategoryVO", "{"layercategoryId":"3929cdd293a24015b621e4b2cb045f74"}");
TLayercategoryVO tLayercategoryVO = new TLayercategoryVO();
tLayercategoryVO.setLayercategoryId("12b21e160297489ebf514b6203616c41");
// postForObject(template, paramMap, url);
// postForEntity(template, paramMap, url);
// exchange(template, paramMap, url);
// postForEntityByObject(template, tLayercategoryVO, url);
exchangeByPostByObject(template,tLayercategoryVO,url);
// get:
exchangeByGet(template, url);
}
public static String postForObject(RestTemplate template, MultiValueMap paramMap, String url){
// 1、使用postForObject请求接口
String result = template.postForObject(url, paramMap, String.class);
System.out.println("result1==================" + result);
return result;
}
public static String postForEntity(RestTemplate template,MultiValueMap paramMap,String url){
HttpHeaders headers = new HttpHeaders();
headers.add("token", "3ecdcf7b78ad48feb64dc6426b8c6a07");
HttpEntity> httpEntity = new HttpEntity>(paramMap,headers);
ResponseEntity response2 = template.postForEntity(url, httpEntity, String.class);
System.out.println("result2====================" + response2.getBody());
return response2.getBody();
}
public static String exchange(RestTemplate template,MultiValueMap paramMap,String url) {
HttpHeaders headers = new HttpHeaders();
headers.add("token", "3ecdcf7b78ad48feb64dc6426b8c6a07");
HttpEntity> httpEntity = new HttpEntity>(paramMap,headers);
ResponseEntity response3 = template.exchange(url, HttpMethod.POST, httpEntity, String.class);
System.out.println("result3====================" + response3.getBody());
return response3.getBody();
}
public static String exchangeByPostByObject(RestTemplate template, Object paramMap,String url) {
HttpHeaders headers = new HttpHeaders();
headers.add("token", "3ecdcf7b78ad48feb64dc6426b8c6a07");
HttpEntity
测试结果
还可以整合ribbon实现负载均衡
参考:http://c.biancheng.net/view/5348.html
https://zhuanlan.zhihu.com/p/262660637
修改 RestTemplate 的配置,增加能够让 RestTemplate 具备负载均衡能力的注解 @LoadBalanced。代码如下所示。
package com.example.restTemplate.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;
@Configuration
public class RestTempLateConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
@LoadBalanced 注解原理
在Ribbon示例中可以看到,Ribbon通过一个@LoadBalanced注解就实现了RestTemplate请求的负载均衡,那么他的原理是什么呢?
RestTemplate在发送请求的时候会被ClientHttpRequestInterceptor拦截,LoadBalancerInterceptor是ClientHttpRequestInterceptor的实现类,它的作用就是用于RestTemplate的负载均衡,LoadBalancerInterceptor将负载均衡的核心逻辑交给了loadBalancer



