package com.imyeyu.network; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JacksonException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; 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; import java.lang.reflect.Type; import java.text.SimpleDateFormat; /** * 基于 Jackson 的请求封装。 * * @author 夜雨 * @since 2025-07-15 14:27 */ public class JacksonRequest extends CommonRequest { private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); static { OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); OBJECT_MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL); } 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"); } }