首页 > 其他分享 >RestTempalte用法:使用restTemlplate实现发送http请求

RestTempalte用法:使用restTemlplate实现发送http请求

时间:2022-10-13 20:03:27浏览次数:53  
标签:http String RestTemplate restTemplate restTemlplate RestTempalte class name

前言

最近在项目中遇到了需要发请求给指定接口获取数据的需求,翻了下项目中的代码,发现这类功能都是基于RestTemplate实现的,也有对RestTemplate进行进一步的封装,简化使用的,五花八门,正好以前也没有接触过这方面,就简单学习记录一下RestTemlplate的用法。

RestTemplate的介绍

RestTemplate是一个执行HTTP请求的同步阻塞式工具类,它仅仅只是在 HTTP 客户端库(例如 JDK HttpURLConnectionApache HttpComponentsokHttp 等)基础上,封装了更加简单易用的模板方法 API,方便程序员利用已提供的模板方法发起网络请求和处理........

读完可以发现,其实就是在HttpClient的基础上进行了简单的封装,便于调用。

使用方式:

首先增加依赖,主要是httpClient的依赖以及spring-boot-starter-web依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>
        <!-- httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

然后写配置类,配置RestTemplate

package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

/**
 * @ClassName RestTemplateConfig
 * @Description restTemplate Config
 * @Author xxxx
 * @Date 2022/10/11
 */
@Configuration
public class RestTemplateConfig {
    /**
     * restTemplate 中文乱码给支
     */
    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        RestTemplate restTemplate = new RestTemplate(factory);
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return restTemplate;
    }

    /**
     * 提供创建restTemplate
     *
     * @return SimpleClientHttpRequestFactory
     */
    @Bean
    public ClientHttpRequestFactory factory() {
        SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();
        simpleClientHttpRequestFactory.setReadTimeout(5000); // 读取超时时间
        simpleClientHttpRequestFactory.setConnectTimeout(5000); // 链接超时时间
        return simpleClientHttpRequestFactory;
    }
}

代码很简单,主要是利用@Bean 创建 ClientHttpRequentFactory 利用创建的factory去创建并配置 RestTemplate实例。

配置完毕后,直接可以开始用了,在RestTemplate 中,常用的几类方法有以下几种,与http请求的种类相对应。

1、get

利用resttmplate 发送 get请求主要有以下几种:

  首先是getForEntity 这个方法主要关注的是整个返回实体,可以通过返回的实体获取 body,statuscode以及header等信息。

        // getForEntity 方法,返回指定的实体
        ResponseEntity<String> responseEntity = restTemplate.getForEntity("", String.class);
        // 获取body 等返回数据
        String body = responseEntity.getBody();
        HttpStatus statusCode = responseEntity.getStatusCode();
        int statusCodeValue = responseEntity.getStatusCodeValue();
        HttpHeaders headers = responseEntity.getHeaders();

  其中参数传递的方式也有几种常见的形式,如下所示

        // get传参
        // 1、 数字作为占位符 getForEntity 第三个参数 传对应的参数
        ResponseEntity<String> responseEntity1 = restTemplate.getForEntity("http://localhost/index?name={1}", String.class, "张三");
        System.out.println(responseEntity.getBody());
        // 2、 通过map传参  key为占位符留置的参数,v 为需要传递的参数
        Map<String, String> map = new HashMap<>();
        map.put("name", "李四");
        ResponseEntity<String> responseEntity2 = restTemplate.getForEntity("http://localhost/index?name={name}", String.class, map);
        // 可以使用uri作为第一个参数
        UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://localhost/index?name={name}").build().expand("张三").encode();
        URI uri = uriComponents.toUri();
        ResponseEntity<String> forEntity = restTemplate.getForEntity(uri, String.class);
        // 返回自定义对象 User
        ResponseEntity<User> forEntity1 = restTemplate.getForEntity(uri, User.class);

  可以看出,就和我们平时用controller接收请求时,设置的@GetMapping 一样,支持直接将参数拼接到url上,也可以用map存放,但是key必须要和预留的{xxx} 相对应,否则参数就放不上去了

  第二类get请求的发送方式则是关注与body对象,其只会返回body对象,拿不到http状态码,如下所示:

        // getForObject 只返回body对象
        String forObject = restTemplate.getForObject(uri, String.class);
        User forObject1 = restTemplate.getForObject("http://localhost/index?name={name}", User.class, map);
        User forObject2 = restTemplate.getForObject("http://localhost/index?name={1}", User.class, "lily");

  该类方法支持自定义对象的传递,在发送请求时,给出返回对象的class即可。

2、post

  post方式传参,则是可以通过body传递json字符串,实现大量传递参数,还可以实现自定义header 信息,cookie信息等;post同样与get相类似,有postForEntity 以及postForObject两大类方法。

        // body 参数
        //设置请求参数
        Map<String, Object> param = new HashMap<>();
        param.put("name", "lily");
        param.put("age", 24);
        HttpHeaders headers1 = new HttpHeaders();
        // 自定义 header
        headers1.add("fi-token", "xxx");
        headers1.add("xxx", "xxx");
        // 自定义cookie
        List<String> cookies = new ArrayList<>();
        cookies.add("tk=dlfjdfodfikk3k33kk3k3434343dfdf");
        // 直接放进headers 中
        headers1.put(HttpHeaders.COOKIE, cookies);
        // 设置 body数据类型为json
        headers1.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity httpEntity = new HttpEntity(MAPPER.writeValueAsString(param), headers);
        // url  request  responseType uriva
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity("http://localhost/index2", httpEntity, String.class);
        // 同样关注 body里的信息
        String s = restTemplate.postForObject("http://localhost/index2", httpEntity, String.class);

3、exchange

  exchange方法则是能够通过不同的参数设置,实现不同http请求的发送,算是一个通用的方法。

        // exchange
        ResponseEntity<String> exchange = restTemplate.exchange("http://localhost/index", HttpMethod.GET, httpEntity, String.class);
        return exchange.getBody();

exchange方法有以下几种重载方法,可以看出他支持输入uri以及url,支持自定义请求体,返回类型,以及uri拼接参数,使用Map传递参数等功能,但是暂时没用上(笑) 以后用上了在补充代码。

 

 

 

参考以及内容来源:

https://www.cnblogs.com/jingzh/p/15981102.html

https://www.jianshu.com/p/35aca2e31f06

https://www.cnblogs.com/54chensongxia/p/11414923.html

标签:http,String,RestTemplate,restTemplate,restTemlplate,RestTempalte,class,name
From: https://www.cnblogs.com/charler/p/16789455.html

相关文章

  • http请求方式-OkHttpClient
    http请求方式-OkHttpClientimportcom.example.core.mydemo.http.OrderReqVO;importokhttp3.*;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importor......
  • http请求方式-RestTemplate
    http请求方式-RestTemplateimportcom.alibaba.fastjson.JSON;importcom.example.core.mydemo.http.OrderReqVO;importorg.springframework.http.HttpEntity;import......
  • http请求方式-CloseableHttpClient
    http请求方式-CloseableHttpClientimportcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONObject;importcom.example.core.mydemo.http.OrderReqVO;imp......
  • http请求方式-HttpClient
    http请求方式-HttpClientimportcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONObject;importcom.example.core.mydemo.http.OrderReqVO;importorg.a......
  • http请求方式-HttpURLConnection
    http请求方式-HttpURLConnectionimportcom.alibaba.fastjson.JSON;importcom.example.core.mydemo.http.OrderReqVO;importorg.springframework.lang.Nullable;im......
  • http 报文
    一、报文格式:一个HTTP请求报文由请求行(requestline)、请求头部(header)、空行和请求数据4个部分组成,具体格式如下;1.请求报文格式起始行<method>      空格   <r......
  • python requests库提示警告:InsecureRequestWarning: Unverified HTTPS request is bei
    在利用requests访问链接,有时有有警告InsecureRequestWarning:UnverifiedHTTPSrequestisbeingmade.Addingcertificatever解决办法:Python3访问HT......
  • 【HTTP】190-http 状态码竟然可以这样记
    标题皮了一下,但是内容应该算是比较用心的,不是直接抄了一下官方文档和一堆抽象的术语,尽量配合实例解释的通俗一些。基本介绍状态码(StatusCode)和原因短语(ReasonPhrase)用于简......
  • 下载https图片工具类
    普通http工具类httpClient访问https图片会报以下错误javax.net.ssl.SSLHandshakeException:java.security.cert.CertificateException:Nosubjectalternativenames......
  • nginx访问控制,用户认证,配置https,zabbix监控nginx状态页面
    nginx访问控制,用户认证,配置https,zabbix监控nginx状态页面nginx访问控制用于location段allow:设定允许哪台或哪些主机访问,多个参数间用空格隔开deny:设定禁止哪台或哪些......