在测试SpringAi的时候,发现springAI比较人性化的地方,他为开发者提供了多种请求方式,如下图所示:
上边的三种方式里边,我还是喜欢CURL,巧了,我还没在Springboot框架中使用过CURL呢。正好封装一个CURL工具类。
我这里使用httpclient来实现CURL请求。
一:添加依赖
不需要任何第三方依赖,对,就是这样。
二:实现请求
1:封装POST请求,代码如下:
/**
* @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();
}
}
}
调用测试:
@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请求,代码如下:
/**
* @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())
{