OkHttp是轻量的 Java 定制小程序开发网络请求框架。
依赖
<dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.14.9</version> </dependency>
- 1
- 2
- 3
- 4
- 5
GET 同步、异步请求
POST表单请求
POSTJSON形式请求
定制小程序开发文件上传下载
定制小程序开发传统情况下在java代码里访问restful服务,一般使用Apache的HttpClient。不过此种方法使用起来太过繁琐。spring提供了一种简单便捷的模板类来进行操作,这就是RestTemplate。
RestTemplate是Spring用于同步client端的核心类,简化了与http服务的通信,并满足Restful原则,程序代码可以给它提供URL,并提取结果。默认情况下,RestTemplate默认依赖jdk的HTTP连接工具。当然你也可以 通过setRequestFactory
属性切换到不同的HTTP源,比如Apache HttpComponents、Netty和OkHttp
。
GET请求
getForObject方法
getForEntity方法
如果开发者需要获取响应头信息的话,那么就需要使用 getForEntity 来发送 HTTP 请求,此时返回的对象是一个 ResponseEntity 的实例。这个实例中包含了响应数据以及响应头。
POST请求
PUT请求
DELETE请求
exchange方法和execute方法
exchange和excute可以通用get、post、put、delete方法。
在内部,RestTemplate默认使用HttpMessageConverter
实例将HTTP消息转换成POJO或者从POJO转换成HTTP消息。默认情况下会注册主mime类型的转换器,但也可以通过setMessageConverters注册其他的转换器。
源码
OKHttpProperties
package com.lsh.configurations;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import java.util.concurrent.TimeUnit;/** * @author :LiuShihao * @date :Created in 2020/12/18 9:38 上午 * @desc :okhttp相关配置信息 */@ConfigurationProperties(prefix = "okhttp.config")@Datapublic class OKHttpProperties { /** * Max Idle Connections */ private Integer maxIdleConnections = 30; /** * 连接持续时间 */ private Long keepAliveDuration = 300L; /** * 复用时间单位 */ private TimeUnit keepAliveDurationTimeUnit = TimeUnit.MINUTES; /** * 连接超时时间 */ private Long connectTimeout = 10L; /** * 连接超时时间单位 */ private TimeUnit connectTimeoutTimeUnit = TimeUnit.SECONDS; /** * 写超时时间 */ private Long writeTimeout = 10L; /** * 写超时时间单位 */ private TimeUnit writeTimeoutTimeUnit = TimeUnit.SECONDS; /** * 读超时时间 */ private Long readTimeout = 10L; /** * 读超时时间单位 */ private TimeUnit readTimeoutTimeUnit = TimeUnit.SECONDS; /** * 线程配置 */ private int corePoolSize = Runtime.getRuntime().availableProcessors(); /** * 最大线程数 */ private int maxPoolSize = Runtime.getRuntime().availableProcessors() * 4;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
RestTemplateConfig
package com.lsh.configurations;import lombok.extern.slf4j.Slf4j;import okhttp3.ConnectionPool;import okhttp3.OkHttpClient;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.http.client.ClientHttpRequestFactory;import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;import org.springframework.web.client.RestTemplate;/** * @author :LiuShihao * @date :Created in 2020/12/18 9:40 上午 * @desc :restTemplate配置 */@Configuration@EnableConfigurationProperties(OKHttpProperties.class)@Slf4jpublic class RestTemplateConfig { @Autowired private OKHttpProperties okHttpProperties; /** * 声明 RestTemplate */ @Bean public RestTemplate restTemplate() { ClientHttpRequestFactory factory = httpRequestFactory(); RestTemplate restTemplate = new RestTemplate(factory); log.info("基于okhttp的RestTemplate构建完成!"); return restTemplate; } /** * 工厂 * @return */ private ClientHttpRequestFactory httpRequestFactory() { return new OkHttp3ClientHttpRequestFactory(okHttpConfigClient()); } /** * 客户端 * @return */ private OkHttpClient okHttpConfigClient() { return new OkHttpClient().newBuilder() .connectionPool(pool()) .connectTimeout(okHttpProperties.getConnectTimeout(), okHttpProperties.getConnectTimeoutTimeUnit()) .readTimeout(okHttpProperties.getReadTimeout(), okHttpProperties.getReadTimeoutTimeUnit()) .writeTimeout(okHttpProperties.getWriteTimeout(), okHttpProperties.getWriteTimeoutTimeUnit()) .hostnameVerifier((hostname, session) -> true) .build(); } /** * 连接池 * @return */ private ConnectionPool pool() { return new ConnectionPool(okHttpProperties.getMaxIdleConnections(), okHttpProperties.getKeepAliveDuration(), okHttpProperties.getKeepAliveDurationTimeUnit()); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67