参考资料:
概述:
RestTemplate是一种模板语言,?可支持多种 Http Client的http的访问
- 基于 JDK HttpURLConnection 的 SimpleClientHttpRequestFactory,默认。
- 基于 Apache HttpComponents Client 的 HttpComponentsClientHttpRequestFactory
- 基于 OkHttp3的OkHttp3ClientHttpRequestFactory。
- 基于 Netty4 的 Netty4ClientHttpRequestFactory。
其中HttpURLConnection 和 HttpClient 为原生的网络访问类,OkHttp3采用了 OkHttp3的框架,Netty4 采用了Netty框架。
SpringBoot整合:
- 如果使用默认的引擎,就添加配置类
@Configuration
public class RestConifg {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
- ?如果使用指定的引擎,如OkHttp3? ,则需要修改配置类为
@Configuration
public class RestConifg {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate(new OkHttp3ClientHttpRequestFactory());
}
}
- 使用的时候可以? @autowire? 来进行调用?
@Autowired
private RestTemplate restTemplate;
- 下面是 getForObject/getForEntity ? postForObject/postForEntity ?exchange 对于get/post方式的请求
- getForObject/getForEntity请求get路径
@RequestMapping("/testRestForGet")
public String testRestForGet(){
//IP可以换成任意IP
String url1 = "http://127.0.0.1:8099/getUrl1/100000";
String url2 = "http://127.0.0.1:8099/getUrl2";
String result = restTemplate.getForObject(url1, String.class);
ResponseEntity<String> entity = restTemplate.getForEntity(url1, String.class);
//参数
Map<String,Object> map = new HashMap<>();
map.put("name","张三");
map.put("pwd","123");
map.put("age",15);
User user = restTemplate.getForObject(url2+"?name={name}&pwd={pwd}&age={age}", User.class, map);
ResponseEntity<User> forEntity = restTemplate.getForEntity(url2+"?name={name}&pwd={pwd}&age={age}", User.class, map);
log.info(result);
log.info(entity.toString());
log.info(entity.getBody().toString());
log.info(user.toString());
log.info(forEntity.toString());
log.info(forEntity.getBody().toString());
return "finish";
}
- ?postForObject/postForEntity请求post请求
@RequestMapping("/testRestForPost")
public String testRestForPost(){
//IP可以换成任意IP
String url1 = "http://127.0.0.1:8099/postUrl1";
String url2 = "http://127.0.0.1:8099/postUrl2";
//post方法必须使用 MultiValueMap 传参 对于单个参数 也可以使用实体但是不能使用单个变量传参 也可以
MultiValueMap<String,Object> paramMap2 = new LinkedMultiValueMap<>();
String userName = "prter";
paramMap2.add("userName",userName);
paramMap2.add("age",15);
User u = new User();
u.setName("王丹丹");
u.setPwd("123456");
u.setAge(15);
User user1 = restTemplate.postForObject(url1, u, User.class);
ResponseEntity<User> entity1 = restTemplate.postForEntity(url1, u, User.class);
User user = restTemplate.postForObject(url2, paramMap2, User.class);
ResponseEntity<User> entity = restTemplate.postForEntity(url2, paramMap2, User.class);
log.info(user.toString());
log.info(entity.toString());
log.info(entity.getBody().toString());
log.info(user1.toString());
log.info(entity1.toString());
log.info(entity1.getBody().toString());
return "finish";
}
//对于请求的方法 @requestBody和@requestParam 混用的情况 则需要另一种请求方式
@RequestMapping("/testRestForPost1")
public String testRestForPost1(){
//IP可以换成任意IP
String url = "http://127.0.0.1:8099/postUrl3";
//申明一个请求头
HttpHeaders headers = new HttpHeaders();
//application/json
headers.setContentType(MediaType.APPLICATION_JSON);
Map<String,Object> map = new HashMap<>();
map.put("userName","prter");
User u = new User();
u.setName("lilei");
u.setPwd("123");
u.setAge(15);
HttpEntity<User> entityParam = new HttpEntity<User>(u,headers);
User user = restTemplate.postForObject(url+"?userName={userName}", entityParam, User.class,map);
log.info(user.toString());
return "finish";
}
//Exchange 既可以进行get访问 也可以进行post访问 并且返回值必须用ResponseEntity包装
@RequestMapping("/testRestForExchangeGet")
public String testRestForExchangeGet(){
String url = "http://127.0.0.1:8099/getUrl2";
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("name","粉红豹");
paramMap.put("pwd","123");
paramMap.put("age",15);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
//HttpEntity 包装了传参
HttpEntity<MultiValueMap<String,Object>> requestEntity = new HttpEntity<>(null,headers);
//ResponseEntity 包装返回结果
ResponseEntity<User> response = restTemplate.exchange(url+"?name={name}&pwd={pwd}&age={age}", HttpMethod.GET,requestEntity,User.class,paramMap);
log.info(response.toString());
log.info(response.getBody().toString());
return "finish";
}
//Exchange 既可以进行get访问 也可以进行post访问 并且返回值必须用ResponseEntity包装
@RequestMapping("/testRestForExchangePost")
public String testRestForExchangePost(){
String url = "http://127.0.0.1:8099/postUrl2";
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
paramMap.add("userName","粉红豹");
paramMap.add("age",15);
//HttpEntity 包装了传参
HttpEntity<MultiValueMap<String,Object>> requestEntity = new HttpEntity<>(paramMap);
//ResponseEntity 包装返回结果
ResponseEntity<User> response = restTemplate.exchange(url, HttpMethod.POST,requestEntity,User.class);
log.info(response.toString());
log.info(response.getBody().toString());
return "finish";
}
详细信息请参照参考demo
|