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 和数据。

相关推荐
非 白3 分钟前
【Java分布式】Nacos注册中心
java·开发语言·nacos·注册中心
吃海鲜的骆驼9 分钟前
服务异步通讯与RabbitMQ
java·分布式·后端·rabbitmq
羱滒12 分钟前
sql调优之数据库开发规范
java·数据库·数据库开发
m0_7482386320 分钟前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
Forget the Dream41 分钟前
设计模式之责任链模式
java·c++·设计模式·责任链模式
jonyleek44 分钟前
「JVS更新日志」低代码、企业会议、智能BI、智能排产2.26更新说明
java·大数据·低代码·数据分析·软件需求
计算机小白一个1 小时前
蓝桥杯 Java B 组之最短路径算法(Dijkstra、Floyd-Warshall)
java·数据结构·算法·蓝桥杯
曼岛_1 小时前
[密码学实战]Java实现SM4加解密(ecb,cbc)及工具验证
java·密码学
菜鸟阿达1 小时前
spring boot 2.7 + seata +微服务 降级失败问题修复
spring boot·后端·微服务
Forget the Dream2 小时前
设计模式之代理模式
java·c++·设计模式·代理模式