SpringBoot(二十一)SpringBoot自定义CURL请求类

在测试SpringAi的时候,发现springAI比较人性化的地方,他为开发者提供了多种请求方式,如下图所示:

上边的三种方式里边,我还是喜欢CURL,巧了,我还没在Springboot框架中使用过CURL呢。正好封装一个CURL工具类。

我这里使用httpclient来实现CURL请求。

一:添加依赖

不需要任何第三方依赖,对,就是这样。

二:实现请求

1:封装POST请求,代码如下:

java 复制代码
/**
 * @name CURL POST 请求
 * @param url     接口地址
 * @param list    NameValuePair(简单名称值对节点类型)类似html中的input
 * @param headers 请求头(默认Content-Type:application/x-www-form-urlencoded; charset=UTF-8)
 * 请求示例
    // 设置请求头
    Map<String, String> headers = new HashMap<>();
    headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36");
    // 设置请求参数
    ArrayList<NameValuePair> list = new ArrayList<>();
    list.add(new BasicNameValuePair("indexName", "test"));
    // 发送请求
    String s = CurlRequest.curlPost("http://localhost:7001/java/elastic/indexIsExists", list, headers);
    System.out.println(s);
 */
public static Map<String, Object> curlPost(String url, ArrayList<NameValuePair> list, Map<String, String> headers)
{
    String StringResult = "";
    CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    if (list != null && !list.isEmpty())
    {
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list, Consts.UTF_8);
        httpPost.setEntity(formEntity);
    }
    httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    if (headers != null && !headers.isEmpty())
    {
        for (String head : headers.keySet())
        {
            httpPost.addHeader(head, headers.get(head));
        }
    }
    CloseableHttpResponse response = null;
    try
    {
        response = closeableHttpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        StringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
        EntityUtils.consume(entity);
    }
    catch (Exception e)
    {
        StringResult = "errorException:" + e.getMessage();
        e.printStackTrace();
    }
    finally
    {
        checkObjectIsNull(closeableHttpClient, response);
    }
    return Function.convertJsonToMap(StringResult);
}
/**
 * 判断对象是否为Null
 * @param closeableHttpClient
 * @param response
 */
private static void checkObjectIsNull(CloseableHttpClient closeableHttpClient, CloseableHttpResponse response)
{
    if(response != null)
    {
        try
        {
            response.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    if (closeableHttpClient != null)
    {
        try
        {
            closeableHttpClient.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

调用测试:

java 复制代码
@GetMapping("index/testsss")
public void testsss()
{
    // 测试CURLPOST
    // 设置请求头
    Map<String, String> headers = new HashMap<>();
    headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36");
    // 设置请求参数
    ArrayList<NameValuePair> list = new ArrayList<>();
    list.add(new BasicNameValuePair("indexName", "test"));
    // 发送请求
    String s = CurlRequest.curlPost("http://localhost:7001/java/elastic/indexIsExists", list, headers);
    System.out.println(s);
}

2:封装GET请求,代码如下:

java 复制代码
/**
 * @param url    获取get请求URL地址(无参数/有参)
 * @param params 拼接参数集合
 * @Description get请求URL拼接参数 & URL编码
 * 请求示例
    // 设置请求参数
    Map<String, String> params = new HashMap<>();
    params.put("search", "服务");
    // 获取请求链接
    String appendUrl = CurlRequest.getCurlGetUrl("http://localhost:7001/java/index/getArticleListByCategory?page=1", params);
    System.out.println(appendUrl);
 */
public static String getCurlGetUrl(String url, Map<String, String> params)
{
    StringBuffer buffer = new StringBuffer(url);
    if (params != null && !params.isEmpty())
    {
        for (String key : params.keySet())
        {
            if (buffer.indexOf("?") >= 0)
            {
                buffer.append("&");
            }
            else
            {
                buffer.append("?");
            }
            String value = "";
            //value = params.get(key);
            try
            {
                 value = URLEncoder.encode(params.get(key), StandardCharsets.UTF_8.name());
            }
            catch (UnsupportedEncodingException e)
            {
                e.printStackTrace();
            }//*/
            buffer.append(key)
                    .append("=")
                    .append(value);
        }
    }
    return buffer.toString();
}

/**
 * @param url     接口地址
 * @param headers 请求头
 * @Description get请求
 * 请求示例
    // 设置请求头
    Map<String, String> headers = new HashMap<>();
    headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36");
    // 设置请求参数
    Map<String, String> params = new HashMap<>();
    params.put("search", "服务");
    // 获取请求链接
    String appendUrl = CurlRequest.getCurlGetUrl("http://localhost:7001/java/index/getArticleListByCategory?page=1", params);
    System.out.println(appendUrl);
    // 发送请求
    String postData = CurlRequest.curlGet(appendUrl,headers);
    System.out.println(postData);
 */
public static String curlGet(String url, Map<String, String> headers)
{
    String StringResult = "";
    CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(url);
    if (headers != null && !headers.isEmpty())
    {
        for (String head : headers.keySet())
        {
            httpGet.addHeader(head, headers.get(head));
        }
    }
    CloseableHttpResponse response = null;
    try
    {
        response = closeableHttpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        StringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8).trim();
        EntityUtils.consume(entity);
    }
    catch (Exception e)
    {
        e.printStackTrace();
        StringResult = "errorException:" + e.getMessage();
    }
    finally
    {
        checkObjectIsNull(closeableHttpClient, response);
    }
    return StringResult;
}

/**
 * 判断对象是否为Null
 * @param closeableHttpClient
 * @param response
 */
private static void checkObjectIsNull(CloseableHttpClient closeableHttpClient, CloseableHttpResponse response)
{
    if(response != null)
    {
        try
        {
            response.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    if (closeableHttpClient != null)
    {
        try
        {
            closeableHttpClient.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

测试一下:

java 复制代码
@GetMapping("index/testsss")
public void testsss()
{
// 测试CURLGET
// 设置请求头
Map<String, String> getheaders = new HashMap<>();
getheaders.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36");
// 设置请求参数
Map<String, String> params = new HashMap<>();
params.put("search", "服务");
// 获取请求链接
String appendUrl = CurlRequest.getCurlGetUrl("http://localhost:7001/java/index/getArticleListByCategory?page=1", params);
System.out.println(appendUrl);
// 发送请求
String postData = CurlRequest.curlGet(appendUrl,getheaders);
System.out.println(postData);//*/
}

3:POST异步请求

java 复制代码
/**
 * @name 异步 CURL POST 请求
 * @param url     接口地址
 * @param list    NameValuePair(简单名称值对节点类型)类似html中的input
 * @param headers 请求头(默认Content-Type:application/x-www-form-urlencoded; charset=UTF-8)
 * 请求示例
    ArrayList<NameValuePair> asynclist = new ArrayList<>();
    asynclist.add(new BasicNameValuePair("indexName", "test"));
    asynclist.add(new BasicNameValuePair("id", "90"));
    // 设置请求头
    Map<String, String> asyncHeaders = new HashMap<>();
    asyncHeaders.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36");
    // 发送请求
    Map<String,Object> async = CurlRequest.asyncCurlPost("http://localhost:7001/java/elastic/documentIsExists", asynclist, asyncHeaders);
    System.out.println(async);
    return "我在tessss方法中......";
 */
public static Map<String, Object> asyncCurlPost(String url, ArrayList<NameValuePair> list, Map<String, String> headers)
{
    String responseBody = "";
    // 创建异步HTTP客户端
    CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
    try
    {
        httpClient.start(); // 启动HttpClient

        // 创建Http Post请求
        HttpPost httpPost = new HttpPost(url);
        // 设置请求参数
        if (list != null && !list.isEmpty())
        {
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list, Consts.UTF_8);
            httpPost.setEntity(formEntity);
        }
        // 设置请求头
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        if (headers != null && !headers.isEmpty())
        {
            for (String head : headers.keySet())
            {
                httpPost.addHeader(head, headers.get(head));
            }
        }
        // 发送请求并获取Future对象
        Future<HttpResponse> future = httpClient.execute(httpPost, null);

        // 获取响应
        HttpResponse response = future.get();
        responseBody = EntityUtils.toString(response.getEntity());
        System.out.println("Response: " + responseBody);
    }
    catch (Exception e)
    {
        e.printStackTrace();
        System.out.println("请求出错了!");
    }
    finally
    {
        // 关闭HttpClient
        try
        {
            httpClient.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        Map<String, Object> result = new HashMap<>();
        result.put("code", 1);
        result.put("result", "异步请求发送成功!");
        result.put("responseBody", responseBody);
        return result;
    }
}

测试一下:

java 复制代码
@GetMapping("index/testsss")
public void testsss()
{
    // 测试ASYNCCURLPOST
    // 发送请求
    Map<String,Object> async = CurlRequest.asyncCurlPost("http://localhost:7001/java/index/tessss", null, null);
    System.out.println(async);
}

/**
 * 测试CURL POST异步方法
 */
@PostMapping("index/tessss")
public String tessss()
{
    ArrayList<NameValuePair> asynclist = new ArrayList<>();
    asynclist.add(new BasicNameValuePair("indexName", "test"));
    asynclist.add(new BasicNameValuePair("id", "90"));
    // 设置请求头
    Map<String, String> asyncHeaders = new HashMap<>();
    asyncHeaders.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36");
    // 发送请求
    Map<String,Object> async = CurlRequest.asyncCurlPost("http://localhost:7001/java/elastic/documentIsExists", asynclist, asyncHeaders);
    System.out.println(async);
    return "我在tessss方法中......";
}

浏览器访问:http://localhost:7001/java/index/testsss

控制台输出:

我们先请求这个方法tessss,这个tessss方法的返回值反而后边输出,因此,异步请求成功

三:完整CURL请求类

java 复制代码
package com.springbootblog.utils;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * 自定义curl类
 */
public class CurlRequest
{
    /**
     * @name CURL POST 请求
     * @param url     接口地址
     * @param list    NameValuePair(简单名称值对节点类型)类似html中的input
     * @param headers 请求头(默认Content-Type:application/x-www-form-urlencoded; charset=UTF-8)
     * 请求示例
        // 设置请求头
        Map<String, String> headers = new HashMap<>();
        headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36");
        // 设置请求参数
        ArrayList<NameValuePair> list = new ArrayList<>();
        list.add(new BasicNameValuePair("indexName", "test"));
        // 发送请求
        String s = CurlRequest.curlPost("http://localhost:7001/java/elastic/indexIsExists", list, headers);
        System.out.println(s);
     */
    public static Map<String, Object> curlPost(String url, ArrayList<NameValuePair> list, Map<String, String> headers)
    {
        String StringResult = "";
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        if (list != null && !list.isEmpty())
        {
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list, Consts.UTF_8);
            httpPost.setEntity(formEntity);
        }
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        if (headers != null && !headers.isEmpty())
        {
            for (String head : headers.keySet())
            {
                httpPost.addHeader(head, headers.get(head));
            }
        }
        CloseableHttpResponse response = null;
        try
        {
            response = closeableHttpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            StringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
            EntityUtils.consume(entity);
        }
        catch (Exception e)
        {
            StringResult = "errorException:" + e.getMessage();
            e.printStackTrace();
        }
        finally
        {
            checkObjectIsNull(closeableHttpClient, response);
        }
        return Function.convertJsonToMap(StringResult);
    }

    /**
     * @name 异步 CURL POST 请求
     * @param url     接口地址
     * @param list    NameValuePair(简单名称值对节点类型)类似html中的input
     * @param headers 请求头(默认Content-Type:application/x-www-form-urlencoded; charset=UTF-8)
     * 请求示例
        ArrayList<NameValuePair> asynclist = new ArrayList<>();
        asynclist.add(new BasicNameValuePair("indexName", "test"));
        asynclist.add(new BasicNameValuePair("id", "90"));
        // 设置请求头
        Map<String, String> asyncHeaders = new HashMap<>();
        asyncHeaders.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36");
        // 发送请求
        Map<String,Object> async = CurlRequest.asyncCurlPost("http://localhost:7001/java/elastic/documentIsExists", asynclist, asyncHeaders);
        System.out.println(async);
        return "我在tessss方法中......";
     */
    public static Map<String, Object> asyncCurlPost(String url, ArrayList<NameValuePair> list, Map<String, String> headers)
    {
        String responseBody = "";
        // 创建异步HTTP客户端
        CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
        try
        {
            httpClient.start(); // 启动HttpClient

            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            // 设置请求参数
            if (list != null && !list.isEmpty())
            {
                UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list, Consts.UTF_8);
                httpPost.setEntity(formEntity);
            }
            // 设置请求头
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            if (headers != null && !headers.isEmpty())
            {
                for (String head : headers.keySet())
                {
                    httpPost.addHeader(head, headers.get(head));
                }
            }
            // 发送请求并获取Future对象
            Future<HttpResponse> future = httpClient.execute(httpPost, null);

            // 获取响应
            HttpResponse response = future.get();
            responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("Response: " + responseBody);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("请求出错了!");
        }
        finally
        {
            // 关闭HttpClient
            try
            {
                httpClient.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            Map<String, Object> result = new HashMap<>();
            result.put("code", 1);
            result.put("result", "异步请求发送成功!");
            result.put("responseBody", responseBody);
            return result;
        }
    }

    /**
     * @name 组装 GET 请求连接
     * @param url    获取get请求URL地址(无参数/有参)
     * @param params 拼接参数集合
     * @Description get请求URL拼接参数 & URL编码
     * 请求示例
        // 设置请求参数
        Map<String, String> params = new HashMap<>();
        params.put("search", "服务");
        // 获取请求链接
        String appendUrl = CurlRequest.getCurlGetUrl("http://localhost:7001/java/index/getArticleListByCategory?page=1", params);
        System.out.println(appendUrl);
     */
    public static String getCurlGetUrl(String url, Map<String, String> params)
    {
        StringBuffer buffer = new StringBuffer(url);
        if (params != null && !params.isEmpty())
        {
            for (String key : params.keySet())
            {
                if (buffer.indexOf("?") >= 0)
                {
                    buffer.append("&");
                }
                else
                {
                    buffer.append("?");
                }
                String value = "";
                //value = params.get(key);
                try
                {
                     value = URLEncoder.encode(params.get(key), StandardCharsets.UTF_8.name());
                }
                catch (UnsupportedEncodingException e)
                {
                    e.printStackTrace();
                }//*/
                buffer.append(key)
                        .append("=")
                        .append(value);
            }
        }
        return buffer.toString();
    }

    /**
     * @name CURL GET 请求
     * @param url     接口地址
     * @param headers 请求头
     * @Description get请求
     * 请求示例
        // 设置请求头
        Map<String, String> headers = new HashMap<>();
        headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36");
        // 设置请求参数
        Map<String, String> params = new HashMap<>();
        params.put("search", "服务");
        // 获取请求链接
        String appendUrl = CurlRequest.getCurlGetUrl("http://localhost:7001/java/index/getArticleListByCategory?page=1", params);
        System.out.println(appendUrl);
        // 发送请求
        String postData = CurlRequest.curlGet(appendUrl,headers);
        System.out.println(postData);
     */
    public static Map<String, Object> curlGet(String url, Map<String, String> headers)
    {
        String StringResult = "";
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        if (headers != null && !headers.isEmpty())
        {
            for (String head : headers.keySet())
            {
                httpGet.addHeader(head, headers.get(head));
            }
        }
        CloseableHttpResponse response = null;
        try
        {
            response = closeableHttpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            StringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8).trim();
            EntityUtils.consume(entity);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            StringResult = "errorException:" + e.getMessage();
        }
        finally
        {
            checkObjectIsNull(closeableHttpClient, response);
        }
        return Function.convertJsonToMap(StringResult);
    }

    /**
     * 判断对象是否为Null
     * @param closeableHttpClient
     * @param response
     */
    private static void checkObjectIsNull(CloseableHttpClient closeableHttpClient, CloseableHttpResponse response)
    {
        if(response != null)
        {
            try
            {
                response.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        if (closeableHttpClient != null)
        {
            try
            {
                closeableHttpClient.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

}

有好的建议,请在下方输入你的评论。

相关推荐
码上一元1 小时前
SpringBoot自动装配原理解析
java·spring boot·后端
计算机-秋大田1 小时前
基于微信小程序的养老院管理系统的设计与实现,LW+源码+讲解
java·spring boot·微信小程序·小程序·vue
魔道不误砍柴功3 小时前
简单叙述 Spring Boot 启动过程
java·数据库·spring boot
失落的香蕉3 小时前
C语言串讲-2之指针和结构体
java·c语言·开发语言
枫叶_v3 小时前
【SpringBoot】22 Txt、Csv文件的读取和写入
java·spring boot·后端
wclass-zhengge3 小时前
SpringCloud篇(配置中心 - Nacos)
java·spring·spring cloud
路在脚下@3 小时前
Springboot 的Servlet Web 应用、响应式 Web 应用(Reactive)以及非 Web 应用(None)的特点和适用场景
java·spring boot·servlet
黑马师兄3 小时前
SpringBoot
java·spring
数据小小爬虫4 小时前
如何用Java爬虫“偷窥”淘宝商品类目API的返回值
java·爬虫·php
暮春二十四4 小时前
关于用postman调用接口成功但是使用Java代码调用却失败的问题
java·测试工具·postman