本文主要是介绍springboot中使用okhttp3的小结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异...
在 Spring Boot 项目中使用 OkHttp3
进行 HTTP 请求是一个高效且流行的方式。OkHttp3
是一个 Java HTTP 客户端,可以处理各种请求类型,比如 GET、POST、PUT 等,并且支持高效的 HTTP 连接池、请求和响应缓存、以及异步请求处理等。
maven项目中首先是需要引入pom文件:
<dependencies> <!-- OkHttp3 dependency --> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.9.0</version> <!-- 使用最新的稳定版本 --> </dependency> </dependencies>
创建OkHttpClient配置类
接下来,你可以在 Spring Boot 中创建一个配置类,用于配置 OkHttpClient
实例。这是为了方便地进行请求处理和注入到 Spring 中。如果项目中用的地方比较多就封装成一个bean在使用的时候通过spring的注入方式即可使用,如果是临时用一下可以直接在class中创建,通过static代码块初始化。
注册bean的方式:
@Configuration public class OkHttpConfig { // 创建 OkHttpClient Bean @Bean public OkHttpClient okHttpClient() { //可以引入线程池单独线程调用 CONNECTION_POOL = new ConnectionPool(1024, 5, TimeUnit.MINUTES); OK_HTTP_CLIENT = new OkHttpClient.Builder() .connectionPool(CONNECTION_POOL) .connectTimeout(10, TimeUnit.SECONDS) .readTimeout(1, TimeUnit.MINUTES) .retryOnConnectionFailure(true) .pingInterval(Duration.ofSeconds(59)) .build(); } }
在class类中直接使用:
@Slf4j @Service public class http3Test { // 余额最大空闲时间 private final static int idleTime = 60 * 60 * 24; private static final OkHttpClient OK_HTTP_CLIENT; private static final ConnectionPool CONNECTION_POOL; static { CONNECTION_POOL = new ConnectionPool(1024, 5, TimeUnit.MINUTEChina编程S); OK_HTTP_CLIENT = new OkHttpClient.Builder() .connectionPool(CONNECTION_POOL) .connectTimeout(10, TimeUnit.SECONDS) .readTimeout(1, TimeUnit.MINUTES) .retryOnConnectionFailure(true) .pingInterval(Duration.ofSeconds(59)) .build(); }
发送get请求:
@Service public class OkHttpService { @Autowired private OkHttpClient okHttpClient; public String sendGetRequest(String url) throws IOException { // 构建请求 Request request = new Request.Builder() www.chinasem.cn .url(url) .build(); // 执行请求并获取响应 try (Response response = okHttpClient.newCall(request).execute()) { if (response.isSuccessful()) { // 返回响应体内容 return response.body().string(); } else { return "Request failed with code: " + response.code(); } } } }
发送post请求:
@Service public class OkHttpService { @Autowired private OkHttpClient okHttpClient; public String sendPostRequest(String url, String jsonBody) throws IOException { // 创建请求体 RequestBody body = RequestBody.create(jsonBody, MediaType.get("application/json; charset=utf-8")); // 构建请求 Request request = new Request.Builder() .url(url) .post(body) .build(); python // 执行请求并获取响应 try (Response response = okHttpClient.newCall(request).execute()) { if (response.isSuccessful()) { // 返回响应体内容 return response.body().string(); } else { return "Request failed with code: " + response.code(); } } } }
另外还可以发送异步请求,不需要使用使用 execute调用http3提供的enqueue方法
:
import okhttp3.*; public class AsyncOkHttjavascriptpService { private OkHttpClient okHttpClient = new OkHttpClient(); public void sendAsyncGetRequest(String url) { Request request = new Request.Builder().url(url).build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { System.out.println("Request failed: " + e.getMessage()); } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { System.out.println("Response: " + response.body().string()); } else { System.out.println("Request failed with code: " + response.code()); 编程China编程 } } }); } }
到此这篇关于springboot中使用okhttp3的小结的文章就介绍到这了,更多相关springboot使用okhttp3内容请搜索China编程(www.chinasem.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持China编程(www.chinasem.cn)!
这篇关于springboot中使用okhttp3的小结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!