java springboot 集成 okHttp 发送get、post请求

在 Java Spring Boot 项目中可以很方便地集成OkHttp来发送GETPOST请求。以下是具体步骤:

一、添加依赖

在项目的pom.xml文件中添加OkHttp的依赖:

xml 复制代码
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.11.0</version>
</dependency>

二、发送 GET 请求

  1. 创建一个工具类来封装OkHttp的请求方法:
java 复制代码
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

public class OkHttpUtil {

    public static String sendGetRequest(String url) throws IOException {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
               .url(url)
               .build();
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }
            return response.body().string();
        }
    }
}
  1. 在需要发送GET请求的地方调用这个方法:
java 复制代码
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        try {
            String response = OkHttpUtil.sendGetRequest("https://api.example.com/data");
            System.out.println(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

三、发送 POST 请求

  1. 在工具类中添加发送POST请求的方法:
java 复制代码
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import java.io.IOException;

public class OkHttpUtil {
    //... 前面的 sendGetRequest 方法

    public static String sendPostRequest(String url, String jsonData) throws IOException {
        OkHttpClient client = new OkHttpClient();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(jsonData, mediaType);
        Request request = new Request.Builder()
               .url(url)
               .post(body)
               .build();
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }
            return response.body().string();
        }
    }
}
  1. 在需要发送POST请求的地方调用这个方法:
java 复制代码
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        try {
            String jsonData = "{\"key\":\"value\"}";
            String response = OkHttpUtil.sendPostRequest("https://api.example.com/post-endpoint", jsonData);
            System.out.println(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在上述代码中,sendGetRequest方法用于发送GET请求,sendPostRequest方法用于发送POST请求并传入要发送的 JSON 数据。请确保在实际使用中处理可能出现的异常情况,并根据实际需求调整请求的 URL 和数据。

相关推荐
栈与堆24 分钟前
LeetCode 19 - 删除链表的倒数第N个节点
java·开发语言·数据结构·python·算法·leetcode·链表
一路向北·重庆分伦26 分钟前
03-01:MQ常见问题梳理
java·开发语言
一 乐27 分钟前
绿色农产品销售|基于springboot + vue绿色农产品销售系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端·宠物
lhrimperial33 分钟前
企业智能知识库助手落地实践:从RAG到Multi-Agent
java·spring cloud·微服务·系统架构·知识图谱
3***688438 分钟前
Spring Boot中使用Server-Sent Events (SSE) 实现实时数据推送教程
java·spring boot·后端
C***u17641 分钟前
Spring Boot问题总结
java·spring boot·后端
Elieal1 小时前
5 种方式快速创建 SpringBoot 项目
java·spring boot·后端
c***69301 小时前
Spring Boot实时推送技术详解:三个经典案例
spring boot·后端·状态模式
better_liang1 小时前
每日Java面试场景题知识点之-Java修饰符
java·访问控制·static·abstract·final·修饰符·企业级开发
rgeshfgreh1 小时前
Spring事务传播机制深度解析
java·前端·数据库