太原网站关键词优化,手机网站搭建用什么软件?,html网站开发图片素材,如何申请一个免费域名1. RestTemplate是什么#xff1f;
RestTemplate是由Spring框架提供的一个可用于应用中调用rest服务的类它简化了与http服务的通信方式。 RestTemplate是一个执行HTTP请求的同步阻塞式工具类#xff0c;它仅仅只是在 HTTP 客户端库#xff08;例如 JDK HttpURLConnection
RestTemplate是由Spring框架提供的一个可用于应用中调用rest服务的类它简化了与http服务的通信方式。 RestTemplate是一个执行HTTP请求的同步阻塞式工具类它仅仅只是在 HTTP 客户端库例如 JDK HttpURLConnectionApache HttpComponentsokHttp 等基础上封装了更加简单易用的模板方法 API方便程序员利用已提供的模板方法发起网络请求和处理能很大程度上提升我们的开发效率。
2. HttpClient、OKhttp、RestTemplate对比
HttpClient代码复杂还得操心资源回收等。代码很复杂冗余代码多不建议直接使用。
创建HttpClient对象。
创建请求方法的实例并指定请求URL。如果需要发送GET请求创建HttpGet对象如果需要发送POST请求创建HttpPost对象。
如果需要发送请求参数可调用HttpGet、HttpPost共同的setParams(HetpParams params)方法来添加请求参数对于HttpPost对象而言也可调用setEntity(HttpEntity entity)方法来设置请求参数。
调用HttpClient对象的execute(HttpUriRequest request)发送请求该方法返回一个HttpResponse。
调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头调用HttpResponse的getEntity()方法可获取HttpEntity对象该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
释放连接。无论执行方法是否成功都必须释放连接。okhttpOkHttp是一个高效的HTTP客户端允许所有同一个主机地址的请求共享同一个socket连接连接池减少请求延时透明的GZIP压缩减少响应数据的大小缓存响应内容避免一些完全重复的请求。
RestTemplate 是 Spring 提供的用于访问Rest服务的客户端 RestTemplate 提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。
3. 使用RestTemplate
pom
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId
/dependency
初始化
RestTemplate restTemplate new RestTemplate(); //使用了JDK自带的HttpURLConnection
RestTemplate restTemplate new RestTemplate(new HttpComponentsClientHttpRequestFactory());
//集成 HttpClient客户端
RestTemplate restTemplate new RestTemplate(new OkHttp3ClientHttpRequestFactory());
//集成 okhttp常用接口和参数: getForObject返回值直接是响应体内容转为的 JSON 对象 getForEntity返回值的封装包含有响应头, 响应状态码的 ResponseEntity对象 url请求路径 requestEntityHttpEntity对象封装了请求头和请求体 responseType返回数据类型 uriVariables支持PathVariable类型的数据。
4. Get
RestTemplate restTemplate new RestTemplate();
ResponseEntityString entity restTemplate.getForEntity(uri, String.class);
// TODO 处理响应
Test
public void test1() {RestTemplate restTemplate new RestTemplate();String url http://localhost:8080/chat16/test/get;//getForObject方法获取响应体将其转换为第二个参数指定的类型BookDto bookDto restTemplate.getForObject(url, BookDto.class);System.out.println(bookDto);
}Test
public void test2() {RestTemplate restTemplate new RestTemplate();String url http://localhost:8080/chat16/test/get;//getForEntity方法返回值为ResponseEntity类型// ResponseEntity中包含了响应结果中的所有信息比如头、状态、bodyResponseEntityBookDto responseEntity restTemplate.getForEntity(url, BookDto.class);//状态码System.out.println(responseEntity.getStatusCode());//获取头System.out.println(头 responseEntity.getHeaders());//获取bodyBookDto bookDto responseEntity.getBody();System.out.println(bookDto);
}Test
public void test3() {RestTemplate restTemplate new RestTemplate();//url中有动态参数String url http://localhost:8080/chat16/test/get/{id}/{name};MapString, String uriVariables new HashMap();uriVariables.put(id, 1);uriVariables.put(name, SpringMVC系列);//使用getForObject或者getForEntity方法BookDto bookDto restTemplate.getForObject(url, BookDto.class, uriVariables);System.out.println(bookDto);
}Test
public void test4() {RestTemplate restTemplate new RestTemplate();//url中有动态参数String url http://localhost:8080/chat16/test/get/{id}/{name};MapString, String uriVariables new HashMap();uriVariables.put(id, 1);uriVariables.put(name, SpringMVC系列);//getForEntity方法ResponseEntityBookDto responseEntity restTemplate.getForEntity(url, BookDto.class, uriVariables);BookDto bookDto responseEntity.getBody();System.out.println(bookDto);
}5. Post
RestTemplate restTemplate new RestTemplate();
// headers
HttpHeaders headers new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntityObject objectHttpEntity new HttpEntity(headers);ResponseEntityString entity restTemplate.postForEntity(uri, request, String.class);
// TODO 处理响应 public static void main(String[] args) {RestTemplate restTemplate new RestTemplate();//三种方式请求String url https://restapi.amap.com/v3/weather/weatherInfo?city110101key3ff9482454cb60bcb73f65c8c48d4209](https://restapi.amap.com/v3/weather/weatherInfo?city110101key3ff9482454cb60bcb73f65c8c48d4209);MapString,Object paramsnew HashMap();ResponseEntityString result restTemplate.postForEntity(url,params, String.class);System.out.println(result.getStatusCode().getReasonPhrase());System.out.println(result.getStatusCodeValue());System.out.println(result.getBody());}
Autowired
private RestTemplate restTemplate;/*** 模拟表单提交post请求*/
Test
public void testPostByForm(){//请求地址String url http://localhost:8080/testPostByForm;// 请求头设置,x-www-form-urlencoded格式的数据HttpHeaders headers new HttpHeaders();headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);//提交参数设置MultiValueMapString, String map new LinkedMultiValueMap();map.add(userName, 唐三藏);map.add(userPwd, 123456);// 组装请求体HttpEntityMultiValueMapString, String request new HttpEntity(map, headers);//发起请求ResponseBean responseBean restTemplate.postForObject(url, request, ResponseBean.class);System.out.println(responseBean.toString());
}
6. Put
Autowired
private RestTemplate restTemplate;
/*** 模拟JSON提交put请求*/
Test
public void testPutByJson(){//请求地址String url http://localhost:8080/testPutByJson;//入参RequestBean request new RequestBean();request.setUserName(唐三藏);request.setUserPwd(123456789);//模拟JSON提交put请求restTemplate.put(url, request);
}
7. Delete
Autowired
private RestTemplate restTemplate;
/*** 模拟JSON提交delete请求*/
Test
public void testDeleteByJson(){//请求地址String url http://localhost:8080/testDeleteByJson;//模拟JSON提交delete请求restTemplate.delete(url);
}