diff --git a/pom.xml b/pom.xml index 80157a7..0bc3d87 100644 --- a/pom.xml +++ b/pom.xml @@ -94,20 +94,15 @@ timi-io 0.0.2 - - org.apache.commons - commons-collections4 - 4.4 - org.apache.httpcomponents.client5 httpclient5-fluent 5.6 - com.google.code.gson - gson - 2.13.2 + tools.jackson.core + jackson-databind + 3.1.0 org.projectlombok diff --git a/src/main/java/com/imyeyu/network/FileRequest.java b/src/main/java/com/imyeyu/network/FileRequest.java index d237676..43acc83 100644 --- a/src/main/java/com/imyeyu/network/FileRequest.java +++ b/src/main/java/com/imyeyu/network/FileRequest.java @@ -17,7 +17,7 @@ import java.nio.file.Path; * @author 夜雨 * @since 2025-06-26 13:03 */ -public class FileRequest extends GsonRequest { +public class FileRequest extends JacksonRequest { protected FileRequest(Request request) { super(request); diff --git a/src/main/java/com/imyeyu/network/GsonRequest.java b/src/main/java/com/imyeyu/network/GsonRequest.java deleted file mode 100644 index 935e8f0..0000000 --- a/src/main/java/com/imyeyu/network/GsonRequest.java +++ /dev/null @@ -1,100 +0,0 @@ -package com.imyeyu.network; - -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.google.gson.reflect.TypeToken; -import com.imyeyu.java.TimiJava; -import org.apache.hc.client5.http.fluent.Request; -import org.apache.hc.core5.http.ContentType; -import org.apache.hc.core5.http.HttpEntity; - -import java.io.IOException; - -/** - * @author 夜雨 - * @since 2025-07-15 14:27 - */ -public class GsonRequest extends CommonRequest { - - private static final Gson GSON = new Gson(); - - private Gson gson = null; - - protected GsonRequest(Request request) { - super(request); - } - - protected Gson getGson() { - return TimiJava.firstNotNull(gson, GSON); - } - - public static GsonRequest wrap(Request request) { - return new GsonRequest(request); - } - - public static GsonRequest get(String url) { - return new GsonRequest(Request.get(url)); - } - - public static GsonRequest post(String url) { - return new GsonRequest(Request.post(url)); - } - - public GsonRequest gson(Gson gson) { - this.gson = gson; - return this; - } - - @Override - public GsonRequest timeout(long ms) { - super.timeout(ms); - return this; - } - - @Override - public GsonRequest header(String key, String value) { - request.addHeader(key, value); - return this; - } - - @Override - public GsonRequest token(String token) { - request.addHeader("Token", token); - return this; - } - - @Override - public GsonRequest language(String langHeader) { - request.addHeader("Accept-Language", langHeader); - return this; - } - - public GsonRequest body(Object object) { - request.bodyString(getGson().toJson(object), ContentType.APPLICATION_JSON); - return this; - } - - @Override - public GsonRequest bodyEntity(HttpEntity entity) { - super.bodyEntity(entity); - return this; - } - - public T resultAs(Class clazz) throws IOException { - return getGson().fromJson(super.asString(), clazz); - } - - public T resultAs(TypeToken typeToken) throws IOException { - return getGson().fromJson(super.asString(), typeToken); - } - - public JsonObject asJsonObject() throws IOException { - return JsonParser.parseString(super.asString()).getAsJsonObject(); - } - - public JsonArray asJsonArray() throws IOException { - return JsonParser.parseString(super.asString()).getAsJsonArray(); - } -} diff --git a/src/main/java/com/imyeyu/network/JacksonRequest.java b/src/main/java/com/imyeyu/network/JacksonRequest.java new file mode 100644 index 0000000..4126f8a --- /dev/null +++ b/src/main/java/com/imyeyu/network/JacksonRequest.java @@ -0,0 +1,204 @@ +package com.imyeyu.network; + +import org.apache.hc.client5.http.fluent.Request; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.HttpEntity; +import tools.jackson.core.JacksonException; +import tools.jackson.core.type.TypeReference; +import tools.jackson.databind.JsonNode; +import tools.jackson.databind.ObjectMapper; +import tools.jackson.databind.node.ArrayNode; +import tools.jackson.databind.node.ObjectNode; + +import java.io.IOException; +import java.lang.reflect.Type; + +/** + * 基于 Jackson 的请求封装。 + * + * @author 夜雨 + * @since 2025-07-15 14:27 + */ +public class JacksonRequest extends CommonRequest { + + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + + private ObjectMapper objectMapper = null; + + protected JacksonRequest(Request request) { + super(request); + } + + /** + * 获取当前请求使用的对象映射器。 + * + * @return 对象映射器 + */ + protected ObjectMapper getObjectMapper() { + if (objectMapper != null) { + return objectMapper; + } + return OBJECT_MAPPER; + } + + /** + * 包装现有请求对象。 + * + * @param request 请求对象 + * @return Jackson 请求 + */ + public static JacksonRequest wrap(Request request) { + return new JacksonRequest(request); + } + + /** + * 创建 GET 请求。 + * + * @param url 请求地址 + * @return Jackson 请求 + */ + public static JacksonRequest get(String url) { + return new JacksonRequest(Request.get(url)); + } + + /** + * 创建 POST 请求。 + * + * @param url 请求地址 + * @return Jackson 请求 + */ + public static JacksonRequest post(String url) { + return new JacksonRequest(Request.post(url)); + } + + /** + * 指定对象映射器。 + * + * @param objectMapper 对象映射器 + * @return 当前请求 + */ + public JacksonRequest objectMapper(ObjectMapper objectMapper) { + this.objectMapper = objectMapper; + return this; + } + + @Override + public JacksonRequest timeout(long ms) { + super.timeout(ms); + return this; + } + + @Override + public JacksonRequest header(String key, String value) { + request.addHeader(key, value); + return this; + } + + @Override + public JacksonRequest token(String token) { + request.addHeader("Token", token); + return this; + } + + @Override + public JacksonRequest language(String langHeader) { + request.addHeader("Accept-Language", langHeader); + return this; + } + + /** + * 设置 JSON 请求体。 + * + * @param object 要序列化的对象 + * @return 当前请求 + * @throws IllegalArgumentException JSON 序列化失败时抛出 + */ + public JacksonRequest body(Object object) { + try { + request.bodyString(getObjectMapper().writeValueAsString(object), ContentType.APPLICATION_JSON); + return this; + } catch (JacksonException e) { + throw new IllegalArgumentException("Jackson serialize request body failed", e); + } + } + + @Override + public JacksonRequest bodyEntity(HttpEntity entity) { + super.bodyEntity(entity); + return this; + } + + /** + * 将响应体解析为指定类型。 + * + * @param clazz 目标类型 + * @param 结果类型 + * @return 解析结果 + * @throws IOException 读取或解析失败 + */ + public T resultAs(Class clazz) throws IOException { + return getObjectMapper().readValue(super.asString(), clazz); + } + + /** + * 将响应体解析为指定泛型类型。 + * + * @param typeReference 目标类型引用 + * @param 结果类型 + * @return 解析结果 + * @throws IOException 读取或解析失败 + */ + public T resultAs(TypeReference typeReference) throws IOException { + return getObjectMapper().readValue(super.asString(), typeReference); + } + + /** + * 将响应体解析为指定反射类型。 + * + * @param type 目标类型 + * @param 结果类型 + * @return 解析结果 + * @throws IOException 读取或解析失败 + */ + public T resultAs(Type type) throws IOException { + return getObjectMapper().readValue(super.asString(), getObjectMapper().constructType(type)); + } + + /** + * 将响应体解析为 JSON 节点。 + * + * @return JSON 节点 + * @throws IOException 读取或解析失败 + */ + public JsonNode asJsonNode() throws IOException { + return getObjectMapper().readTree(super.asString()); + } + + /** + * 将响应体解析为 JSON 对象。 + * + * @return JSON 对象节点 + * @throws IOException 读取或解析失败 + */ + public ObjectNode asJsonObject() throws IOException { + JsonNode jsonNode = asJsonNode(); + if (jsonNode instanceof ObjectNode objectNode) { + return objectNode; + } + throw new IOException("Response body is not a JSON object"); + } + + /** + * 将响应体解析为 JSON 数组。 + * + * @return JSON 数组节点 + * @throws IOException 读取或解析失败 + */ + public ArrayNode asJsonArray() throws IOException { + JsonNode jsonNode = asJsonNode(); + if (jsonNode instanceof ArrayNode arrayNode) { + return arrayNode; + } + throw new IOException("Response body is not a JSON array"); + } +} diff --git a/src/main/java/com/imyeyu/network/TimiRequest.java b/src/main/java/com/imyeyu/network/TimiRequest.java index c05d6f3..986604f 100644 --- a/src/main/java/com/imyeyu/network/TimiRequest.java +++ b/src/main/java/com/imyeyu/network/TimiRequest.java @@ -1,17 +1,19 @@ package com.imyeyu.network; -import com.google.gson.reflect.TypeToken; import com.imyeyu.java.bean.timi.TimiResponse; import org.apache.hc.client5.http.fluent.Request; import org.apache.hc.core5.http.HttpEntity; +import tools.jackson.core.type.TypeReference; +import tools.jackson.databind.JavaType; import java.io.IOException; +import java.lang.reflect.Type; /** * @author 夜雨 * @since 2025-07-15 14:34 */ -public class TimiRequest extends GsonRequest { +public class TimiRequest extends JacksonRequest { protected TimiRequest(Request request) { super(request); @@ -75,16 +77,48 @@ public class TimiRequest extends GsonRequest { return Double.parseDouble(asString()); } + /** + * 读取 Timi 响应中的数据字段。 + * + * @param clazz 数据类型 + * @param 数据泛型 + * @return 数据内容 + * @throws IOException 读取或解析失败 + */ public T resultAs(Class clazz) throws IOException { - TimiResponse resp = getGson().fromJson(asJsonObject(), TypeToken.getParameterized(TimiResponse.class, clazz).getType()); + TimiResponse resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(clazz)); if (resp.isFail()) { throw resp.toException(); } return resp.getData(); } - public T resultAs(TypeToken typeToken) throws IOException { - TimiResponse resp = getGson().fromJson(asJsonObject(), TypeToken.getParameterized(TimiResponse.class, typeToken.getType()).getType()); + /** + * 读取 Timi 响应中的泛型数据字段。 + * + * @param typeReference 数据类型引用 + * @param 数据泛型 + * @return 数据内容 + * @throws IOException 读取或解析失败 + */ + public T resultAs(TypeReference typeReference) throws IOException { + TimiResponse resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(typeReference)); + if (resp.isFail()) { + throw resp.toException(); + } + return resp.getData(); + } + + /** + * 读取 Timi 响应中的泛型数据字段。 + * + * @param type 数据类型 + * @param 数据泛型 + * @return 数据内容 + * @throws IOException 读取或解析失败 + */ + public T resultAs(Type type) throws IOException { + TimiResponse resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(type)); if (resp.isFail()) { throw resp.toException(); } @@ -93,9 +127,39 @@ public class TimiRequest extends GsonRequest { @Override public void execute() throws IOException { - TimiResponse resp = getGson().fromJson(asJsonObject(), TypeToken.getParameterized(TimiResponse.class, Object.class).getType()); + TimiResponse resp = getObjectMapper().treeToValue(asJsonObject(), timiResponseType(Object.class)); if (resp.isFail()) { throw resp.toException(); } } + + /** + * 构造带泛型的 Timi 响应类型。 + * + * @param clazz 数据类型 + * @return Jackson 类型 + */ + protected JavaType timiResponseType(Class clazz) { + return getObjectMapper().getTypeFactory().constructParametricType(TimiResponse.class, clazz); + } + + /** + * 构造带泛型的 Timi 响应类型。 + * + * @param typeReference 数据类型引用 + * @return Jackson 类型 + */ + protected JavaType timiResponseType(TypeReference typeReference) { + return getObjectMapper().getTypeFactory().constructParametricType(TimiResponse.class, getObjectMapper().constructType(typeReference)); + } + + /** + * 构造带泛型的 Timi 响应类型。 + * + * @param type 数据类型 + * @return Jackson 类型 + */ + protected JavaType timiResponseType(Type type) { + return getObjectMapper().getTypeFactory().constructParametricType(TimiResponse.class, getObjectMapper().constructType(type)); + } }