Spring boot 整合 Okhttp3 并封装请求工具实例 详解
作者:洛祁枫
OkHttp作为一款成熟、稳定、易用的HTTP客户端库,拥有较高的性能和多样化的功能,已被广泛应用于移动应用开发、Web服务端开发等领域,这篇文章主要介绍了Spring boot 整合 Okhttp3 并封装请求工具,需要的朋友可以参考下
一、 为什么要使用okHttp
OkHttp是一个高效、灵活、易于使用的HTTP客户端库,优势如下:
- 性能更高:OkHttp在网络请求处理上采用了异步模型,并将连接池、压缩、网络协议等多种技术应用到其中,从而提高了网络请求的效率和处理速度。
- 功能更强大:OkHttp支持HTTP/2协议,可以进行数据流复用以及服务器推送。同时,OkHttp还支持GZIP压缩、连接超时设置、缓存、重试等功能,提供了非常丰富的API接口,方便开发者进行扩展和个性化定制。
- 使用更简单:OkHttp具有良好的API设计,可以轻松地实现网络请求的发送和响应处理。其内置了许多预定义的请求类型,如Get, Post, Head, Put, Delete等,使得开发者可以快速地进行开发。
- 兼容性更好:OkHttp的代码精简,运行效率高,并且兼容Android平台和Java平台,可以在各种场景下进行使用。
OkHttp作为一款成熟、稳定、易用的HTTP客户端库,拥有较高的性能和多样化的功能,已被广泛应用于移动应用开发、Web服务端开发等领域。
二、 导入依赖
<!-- okhttp3 依赖 --> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.9.3</version> </dependency>
三、 配置
- 使用okhttp3 是很简单的,但是需要配置连接池,缓存代理等
- 项目配置,application.yml 文件配置
ok: http: connect-timeout: 60 read-timeout: 60 write-timeout: 60 # 连接池中整体的空闲连接的最大数量 max-idle-connections: 200 # 连接空闲时间最多为 300 秒 keep-alive-duration: 300
3.添加 配置应用,连接池,缓存,代理等开启
import okhttp3.ConnectionPool; import okhttp3.OkHttpClient; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.concurrent.TimeUnit; /** * @author luoqifeng */ @Configuration public class OkHttpConfiguration { @Value("${ok.http.connect-timeout}") private Integer connectTimeout; @Value("${ok.http.read-timeout}") private Integer readTimeout; @Value("${ok.http.write-timeout}") private Integer writeTimeout; @Value("${ok.http.max-idle-connections}") private Integer maxIdleConnections; @Value("${ok.http.keep-alive-duration}") private Long keepAliveDuration; @Bean public OkHttpClient okHttpClient() { return new OkHttpClient.Builder() .sslSocketFactory(sslSocketFactory(), x509TrustManager()) // 是否开启缓存 .retryOnConnectionFailure(false) .connectionPool(pool()) .connectTimeout(connectTimeout, TimeUnit.SECONDS) .readTimeout(readTimeout, TimeUnit.SECONDS) .writeTimeout(writeTimeout,TimeUnit.SECONDS) .hostnameVerifier((hostname, session) -> true) // 设置代理 // .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8888))) // 拦截器 // .addInterceptor() .build(); } @Bean public X509TrustManager x509TrustManager() { return new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }; } @Bean public SSLSocketFactory sslSocketFactory() { try { // 信任任何链接 SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[]{x509TrustManager()}, new SecureRandom()); return sslContext.getSocketFactory(); } catch (NoSuchAlgorithmException | KeyManagementException e) { e.printStackTrace(); } return null; } @Bean public ConnectionPool pool() { return new ConnectionPool(maxIdleConnections, keepAliveDuration, TimeUnit.SECONDS); } }
4.配置创建工具类
package cn.wjj.wjjUtils.util; import jakarta.annotation.PostConstruct; import lombok.extern.slf4j.Slf4j; import okhttp3.*; import org.apache.commons.lang3.exception.ExceptionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Map; /** * @author luoqifeng */ @Slf4j @Component public class OkHttpClientUtil { private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); private static final MediaType XML = MediaType.parse("application/xml; charset=utf-8"); private static OkHttpClient okHttpClient; // 为使用静态调用异步注入 @Autowired private OkHttpClient getOkHttpClient; @PostConstruct public void init() { okHttpClient = getOkHttpClient; } /** * get 请求 * @param url 请求url地址 * @return string * */ public static String doGet(String url) { return doGet(url, null, null); } public static byte[] doGetByte(String url) { return doGetByte(url, null, null); } public static String doPost(String url) { return doPost(url, null, null); } /** * get 请求 * @param url 请求url地址 * @param params 请求参数 map * @return string * */ public static String doGetToParams(String url, Map<String, String> params) { return doGet(url, params, null); } /** * get 请求 * @param url 请求url地址 * @param headers 请求头字段 {k1, v1 k2, v2, ...} * @return string * */ public static String doGetToHeaders(String url, Map<String, String> headers) { return doGet(url, null, headers); } /** * get 请求 * @param url 请求url地址 * @param params 请求参数 map * @param headers 请求头字段 {k1, v1 k2, v2, ...} * @return string * */ public static String doGet(String url, Map<String, String> params, Map<String, String> headers) { StringBuilder sb = new StringBuilder(url); if (params != null && params.keySet().size() > 0) { boolean firstFlag = true; for (String key : params.keySet()) { if (firstFlag) { sb.append("?").append(key).append("=").append(params.get(key)); firstFlag = false; } else { sb.append("&").append(key).append("=").append(params.get(key)); } } } Request.Builder builder = new Request.Builder(); if (headers != null && !headers.isEmpty()) { for (String header:headers.keySet()){ builder.addHeader(header, headers.get(header)); } } Request request = builder.url(sb.toString()).build(); log.info("do get request and url[{}]", sb.toString()); return executeBody(request); } public static byte[] doGetByte(String url, Map<String, String> params, Map<String, String> headers) { StringBuilder sb = new StringBuilder(url); if (params != null && params.keySet().size() > 0) { boolean firstFlag = true; for (String key : params.keySet()) { if (firstFlag) { sb.append("?").append(key).append("=").append(params.get(key)); firstFlag = false; } else { sb.append("&").append(key).append("=").append(params.get(key)); } } } Request.Builder builder = new Request.Builder(); if (headers != null && !headers.isEmpty()) { for (String header:headers.keySet()){ builder.addHeader(header, headers.get(header)); } } Request request = builder.url(sb.toString()).build(); log.info("do get request and url[{}]", sb.toString()); return executeByte(request); } /** * post 请求 * @param url 请求url地址 * @param params 请求参数 map * @return string */ public static String doPostForm(String url, Map<String, String> params) { FormBody.Builder builder = new FormBody.Builder(); if (params != null && params.keySet().size() > 0) { for (String key : params.keySet()) { builder.add(key, params.get(key)); } } Request request = new Request.Builder().url(url).post(builder.build()).build(); log.info("do post request and url[{}]", url); return execute(request); } /** * post 请求 * @param url 请求url地址 * @param params 请求参数 map * @param headers 请求头字段 {k1:v1, k2: v2, ...} * @return string */ public static String doPost(String url, Map<String, String> params, Map<String, String> headers) { FormBody.Builder builder = new FormBody.Builder(); if (params != null && params.keySet().size() > 0) { for (String key : params.keySet()) { builder.add(key, params.get(key)); } } Request.Builder requestBuilder = new Request.Builder(); if (headers != null && !headers.isEmpty()) { for (String header:headers.keySet()){ requestBuilder.addHeader(header, headers.get(header)); } } Request request = requestBuilder.url(url).post(builder.build()).build(); log.info("do post request and url[{}]", url); return execute(request); } /** * post 请求, 请求数据为 json 的字符串 * @param url 请求url地址 * @param json 请求数据, json 字符串 * @return string */ public static String doPostJson(String url, String json) { log.info("do post request and url[{}]", url); return executePost(url, json, JSON); } /** * post 请求, 请求数据为 json 的字符串 * @param url 请求url地址 * @param json 请求数据, json 字符串 * @param headers 请求头字段 {k1, v1 k2, v2, ...} * @return string */ public static String doPostJson(String url, String json, Map<String, String> headers) { log.info("do post request and url[{}]", url); RequestBody requestBody = RequestBody.create(json, JSON); Request.Builder builder = new Request.Builder(); if (headers != null && !headers.isEmpty()) { for (String header:headers.keySet()){ builder.addHeader(header, headers.get(header)); } } Request request = builder.url(url).post(requestBody).build(); return execute(request); } /** * post 请求, 请求数据为 xml 的字符串 * @param url 请求url地址 * @param xml 请求数据, xml 字符串 * @return string */ public static String doPostXml(String url, String xml) { log.info("do post request and url[{}]", url); return executePost(url, xml, XML); } private static String executePost(String url, String data, MediaType contentType) { RequestBody requestBody = RequestBody.create(data ,contentType); Request request = new Request.Builder().url(url).post(requestBody).build(); return execute(request); } private static String execute(Request request) { Response response = null; try { response = okHttpClient.newCall(request).execute(); if (response.isSuccessful()) { return response.body().string(); } } catch (Exception e) { log.error(ExceptionUtils.getStackTrace(e)); } finally { if (response != null) { response.close(); } } return ""; } private static String executeBody(Request request) { Response response = null; try { response = okHttpClient.newCall(request).execute(); if (response.body() != null) { return response.body().string(); } } catch (Exception e) { log.error(ExceptionUtils.getStackTrace(e)); } finally { if (response != null) { response.close(); } } return ""; } private static byte[] executeByte(Request request) { Response response = null; try { response = okHttpClient.newCall(request).execute(); if (response.isSuccessful()) { return response.body().bytes(); } } catch (Exception e) { log.error(ExceptionUtils.getStackTrace(e)); } finally { if (response != null) { response.close(); } } return null; } }
四、 测试
/** * @author luoqifeng */ @RestController @RequestMapping("test") public class testController { @GetMapping("testOkhttp") public String testOkhttp(){ String s = OkHttpClientUtil.doGet("https://www.baidu.com"); return s; } }
响应结果:
至此已经完成基本的Spring Boot整合 okhttp3 的示例。并使用okhttp3来发送HTTP请求,然后返回HTTP响应。
到此这篇关于Spring boot 整合 Okhttp3 并封装请求工具的文章就介绍到这了,更多相关Spring boot 整合 Okhttp3内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!