feat:工具类和配置文件的更新
This commit is contained in:
@@ -0,0 +1,21 @@
|
|||||||
|
package com.xiang.xservice.basic.config;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: xiang
|
||||||
|
* @Date: 2025-07-25 15:56
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class GlobalJacksonConfig {
|
||||||
|
@Bean
|
||||||
|
public ObjectMapper objectMapper() {
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
// 设置null字段也序列化
|
||||||
|
mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
|
||||||
|
return mapper;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package com.xiang.xservice.basic.config;
|
||||||
|
|
||||||
|
import org.apache.http.client.config.RequestConfig;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.impl.client.HttpClients;
|
||||||
|
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: xiang
|
||||||
|
* @Date: 2025-07-25 16:31
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class HttpClientConfig {
|
||||||
|
@Bean
|
||||||
|
public HttpComponentsClientHttpRequestFactory clientHttpRequestFactory() {
|
||||||
|
// 连接池管理器
|
||||||
|
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
|
||||||
|
// 整个连接池最大连接数
|
||||||
|
connectionManager.setMaxTotal(100);
|
||||||
|
// 每个主机的最大连接数
|
||||||
|
connectionManager.setDefaultMaxPerRoute(20);
|
||||||
|
|
||||||
|
// 请求配置
|
||||||
|
RequestConfig requestConfig = RequestConfig.custom()
|
||||||
|
// 建立连接的超时时间
|
||||||
|
.setConnectTimeout(5000)
|
||||||
|
// 响应超时时间
|
||||||
|
.setSocketTimeout(10000)
|
||||||
|
// 从连接池获取连接的超时时间
|
||||||
|
.setConnectionRequestTimeout(1000)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 创建 HttpClient
|
||||||
|
CloseableHttpClient httpClient = HttpClients.custom()
|
||||||
|
.setConnectionManager(connectionManager)
|
||||||
|
.setDefaultRequestConfig(requestConfig)
|
||||||
|
// 清理空闲连接
|
||||||
|
.evictIdleConnections(30, TimeUnit.SECONDS)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 配置给 RestTemplate 使用
|
||||||
|
return new HttpComponentsClientHttpRequestFactory(httpClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RestTemplate restTemplate(HttpComponentsClientHttpRequestFactory factory) {
|
||||||
|
return new RestTemplate(factory);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -21,9 +21,23 @@ public class DateUtils {
|
|||||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||||
return LocalDateTime.parse(dateTimeStr, formatter);
|
return LocalDateTime.parse(dateTimeStr, formatter);
|
||||||
}
|
}
|
||||||
|
public static LocalDateTime getTimeFromStr(String str) {
|
||||||
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||||
|
return LocalDateTime.parse(str, formatter);
|
||||||
|
}
|
||||||
|
|
||||||
public static LocalDate getDateFromStr(String date) {
|
public static LocalDate getDateFromStr(String date) {
|
||||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
|
||||||
return LocalDate.parse(date, formatter);
|
return LocalDate.parse(date, formatter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static LocalDateTime getDateTimeFromStr(String dateTime) {
|
||||||
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
|
||||||
|
return LocalDateTime.parse(dateTime, formatter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getDateTimeStr(LocalDateTime dateTime) {
|
||||||
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||||
|
return dateTime.format(formatter);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,12 +9,14 @@ import org.apache.http.client.methods.HttpPost;
|
|||||||
import org.apache.http.entity.StringEntity;
|
import org.apache.http.entity.StringEntity;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.impl.client.HttpClients;
|
||||||
|
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||||
import org.apache.http.util.EntityUtils;
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author: xiang
|
* @Author: xiang
|
||||||
@@ -29,6 +31,31 @@ public class HttpUtils {
|
|||||||
private static final int defaultMaxPerRoute = 500;
|
private static final int defaultMaxPerRoute = 500;
|
||||||
private static final int maxTotal = 2000;
|
private static final int maxTotal = 2000;
|
||||||
|
|
||||||
|
private static final RequestConfig requestConfig = RequestConfig.custom()
|
||||||
|
.setConnectTimeout(connectTimeout)
|
||||||
|
.setSocketTimeout(socketTimeOut)
|
||||||
|
.setConnectionRequestTimeout(connectionRequestTimeout)
|
||||||
|
.build();
|
||||||
|
// 使用连接池
|
||||||
|
private static final PoolingHttpClientConnectionManager connectionManager;
|
||||||
|
|
||||||
|
private static final CloseableHttpClient httpClient;
|
||||||
|
|
||||||
|
static {
|
||||||
|
connectionManager = new PoolingHttpClientConnectionManager();
|
||||||
|
// 最大连接数
|
||||||
|
connectionManager.setMaxTotal(maxTotal);
|
||||||
|
// 每个主机的最大连接数
|
||||||
|
connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
|
||||||
|
|
||||||
|
httpClient = HttpClients.custom()
|
||||||
|
.setConnectionManager(connectionManager)
|
||||||
|
.setDefaultRequestConfig(requestConfig)
|
||||||
|
// 清理空闲连接
|
||||||
|
.evictIdleConnections(30, TimeUnit.SECONDS)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
public static String doPost(String url, Map<String, String> header, String jsonParams) {
|
public static String doPost(String url, Map<String, String> header, String jsonParams) {
|
||||||
RequestConfig requestConfig = RequestConfig.custom()
|
RequestConfig requestConfig = RequestConfig.custom()
|
||||||
// 设置连接超时时间
|
// 设置连接超时时间
|
||||||
@@ -55,6 +82,54 @@ public class HttpUtils {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String doPostV2(String url, Map<String, String> header, String jsonParams) {
|
||||||
|
CloseableHttpResponse response = null;
|
||||||
|
String result = "";
|
||||||
|
try {
|
||||||
|
log.info("HTTP请求,请求参数===>{}", jsonParams);
|
||||||
|
HttpPost httpPost = getPost(url, header, jsonParams);
|
||||||
|
response = httpClient.execute(httpPost);
|
||||||
|
result = EntityUtils.toString(response.getEntity(), "utf-8");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("doPost异常", e);
|
||||||
|
} finally {
|
||||||
|
// 不关闭 httpClient
|
||||||
|
closeResource(response);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String doGetV2(String url, Map<String, String> header, Map<String, String> param) {
|
||||||
|
CloseableHttpResponse response = null;
|
||||||
|
String result = "";
|
||||||
|
try {
|
||||||
|
StringBuilder requestUrl = new StringBuilder(url);
|
||||||
|
if (MapUtils.isNotEmpty(param)) {
|
||||||
|
requestUrl.append("?");
|
||||||
|
param.forEach((k, v) -> requestUrl.append(k).append("=").append(v).append("&"));
|
||||||
|
// 去掉最后一个 &
|
||||||
|
requestUrl.setLength(requestUrl.length() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpGet httpGet = new HttpGet(requestUrl.toString());
|
||||||
|
httpGet.addHeader("Content-Type", "application/json");
|
||||||
|
if (MapUtils.isNotEmpty(header)) {
|
||||||
|
header.forEach(httpGet::setHeader);
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("doGet请求:请求地址:{}", requestUrl);
|
||||||
|
response = httpClient.execute(httpGet);
|
||||||
|
result = EntityUtils.toString(response.getEntity(), "utf-8");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("doGet异常:", e);
|
||||||
|
} finally {
|
||||||
|
closeResource(response);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private static HttpPost getPost(String url, Map<String, String> header, String jsonParams) {
|
private static HttpPost getPost(String url, Map<String, String> header, String jsonParams) {
|
||||||
HttpPost httpPost = new HttpPost(url);
|
HttpPost httpPost = new HttpPost(url);
|
||||||
httpPost.addHeader("Content-Type", "application/json");
|
httpPost.addHeader("Content-Type", "application/json");
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.xiang.xservice.basic.utils;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: xiang
|
||||||
|
* @Date: 2025-07-25 15:58
|
||||||
|
*/
|
||||||
|
public class JsonUtils {
|
||||||
|
public static String toJsonString(Object obj) {
|
||||||
|
return JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T parse(String json, Class<T> clazz) {
|
||||||
|
return JSON.parseObject(json, clazz);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user