REST in Spring 3: RestTemplate
*
*
*
webClient:是Spring-webFlux包下的,非阻塞响应,最低java8支持函数式编程,性能好;【推荐】
RestTemplate:是Spring-webmvc包下的,是阻塞响应,满足RestFul原则,代码简单,默认依赖jdk的HTTP连接工具。【少用】
HttpClient:是apache httpClient包下的,代码复杂,需要资源回收。【不用】
OkHttp:
RestTemplateConfig.java
package com.example.demofastjson.config;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(){
// RestTemplate restTemplate = new RestTemplate();
//设置中文乱码问题方式一
// restTemplate.getMessageConverters().add(1,new StringHttpMessageConverter(Charset.forName("UTF-8")));
// 设置中文乱码问题方式二
// restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8)); // 支持中文编码
return new RestTemplate();
}
}
RestTemplateController.java
package com.example.demofastjson.controller;
import com.example.demofastjson.pojo.UserRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("resttemp")
public class RestTemplateController {
@Autowired
private RestTemplate restTemplate;
/**
* ok
* @return
*/
@GetMapping("find")
public ResponseEntity<String> findByPage(){
ResponseEntity<String> entity = restTemplate.getForEntity("http://localhost:36247/weatherforecast/string", String.class);
System.out.println("状态码:"+entity.getStatusCode());
System.out.println("响应体"+entity.getBody());
return ResponseEntity.ok(entity.getBody());
}
/**
* ok
* @return
*/
@RequestMapping("get1")
public String Get1() {
String response = restTemplate.getForObject("http://localhost:36247/weatherforecast/string",String.class);
return response;
}
/**
* ok
* HashMap 参数
* @return
*/
@RequestMapping("add")
public String add() {
//接口地址
String url = "http://localhost:36247/weatherforecast";
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization","123");
MediaType type=MediaType.parseMediaType("application/json;charset=UTF-8");
headers.setContentType(type);
HashMap<String, Object> map = new HashMap<>();
map.put("name","admin");
map.put("password","123456");
HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map,headers);
ResponseEntity<String> responseResultResponseEntity = restTemplate.postForEntity(url, objectHttpEntity, String.class);
return "123";
}
/**
* ok
* 请求的接口参数是FromBody,不能使用 MultiValueMap
* @return
*/
@RequestMapping("create")
public String create() {
//接口地址
String url = "http://localhost:36247/weatherforecast";
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization","123");
MediaType type=MediaType.parseMediaType("application/json;charset=UTF-8");
headers.setContentType(type);
/**
* 请求的接口参数是FromBody,此处使用 MultiValueMap 会报错
* MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
* paramMap.add("name","admin");
* paramMap.add("password","123456");
*/
UserRequest request = new UserRequest();
request.setName("admin");
request.setPassword("123456");
HttpEntity<UserRequest> objectHttpEntity = new HttpEntity<>(request,headers);
ResponseEntity<String> responseResultResponseEntity = restTemplate.postForEntity(url, objectHttpEntity, String.class);
return "123 " + responseResultResponseEntity.getBody();
}
/**
* ok
* HashMap 参数
* @return
*/
@RequestMapping("exchange")
public String exchange() {
//接口地址
String url = "http://localhost:36247/weatherforecast";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
//headers.add("Content-Type","application/json");
headers.add("Authorization","123");
HashMap<String, Object> map = new HashMap<>();
map.put("name","admin_create");
map.put("password","123456_create");
HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map,headers);
ResponseEntity<String> responseResultResponseEntity = restTemplate.exchange(url, HttpMethod.POST, objectHttpEntity, String.class);
return "123";
}
/**
* ok
* @return
*/
@RequestMapping("delete")
public ResponseEntity<String> deleteGoods(){
restTemplate.delete("http://localhost:36247/weatherforecast",String.class);
return ResponseEntity.ok("删除成功");
}
@RequestMapping("get3")
public String get3() {
String url = "http://localhost:4365/weatherforecast/c?name={name}&password={password}";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.add("Authorization","123");
/**
* ok
* get请求,string Get(string name,string password) 能获取到参数
* get请求,string Get([FromForm] UserRequest request) 不能获取到参数
*/
MultiValueMap<String, Object> paramMap1 = new LinkedMultiValueMap<>();
paramMap1.add("name","admin");
paramMap1.add("password","123456");
/**
* ok
* get请求,string Get(string name,string password) 能获取到参数
* get请求,string Get([FromForm] UserRequest request) 不能获取到参数
*/
Map<String, Object> paramMap2 = new HashMap<>();
paramMap2.put("name", "20181116");
paramMap2.put("password", "10");
/**
* ok
* get请求,string Get(string name,string password) 能获取到参数
* get请求,string Get([FromForm] UserRequest request) 不能获取到参数
*/
Map paramMap3 = new HashMap();
paramMap3.put("name","4d6ab733dcfed0e82806b9a97ff602ff");
paramMap3.put("password","330100");
/**
* ok
* get请求,string Get(string name,string password) 能获取到参数
* get请求,string Get([FromForm] UserRequest request) 不能获取到参数
*/
Map<String,String> paramMap4=new HashMap<>();
paramMap4.put("name","DFCFMonitor");
paramMap4.put("password","123123123123");
restTemplate.getForEntity(url,String.class,paramMap4);
return "123";
}
}
*
*
*