Class JacksonRequest

java.lang.Object
com.imyeyu.network.CommonRequest
com.imyeyu.network.JacksonRequest
Direct Known Subclasses:
FileRequest, TimiRequest

public class JacksonRequest extends CommonRequest
基于 Jackson 的请求封装。
Since:
2025-07-15 14:27
Author:
夜雨
  • Constructor Details

    • JacksonRequest

      protected JacksonRequest(org.apache.hc.client5.http.fluent.Request request)
  • Method Details

    • getObjectMapper

      protected com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
      获取当前请求使用的对象映射器。
      Returns:
      对象映射器
    • wrap

      public static JacksonRequest wrap(org.apache.hc.client5.http.fluent.Request request)
      包装现有请求对象。
      Parameters:
      request - 请求对象
      Returns:
      Jackson 请求
    • get

      public static JacksonRequest get(String url)
      创建 GET 请求。
      Parameters:
      url - 请求地址
      Returns:
      Jackson 请求
    • post

      public static JacksonRequest post(String url)
      创建 POST 请求。
      Parameters:
      url - 请求地址
      Returns:
      Jackson 请求
    • objectMapper

      public JacksonRequest objectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
      指定对象映射器。
      Parameters:
      objectMapper - 对象映射器
      Returns:
      当前请求
    • timeout

      public JacksonRequest timeout(long ms)
      Overrides:
      timeout in class CommonRequest
    • header

      public JacksonRequest header(String key, String value)
      Overrides:
      header in class CommonRequest
    • token

      public JacksonRequest token(String token)
      Overrides:
      token in class CommonRequest
    • language

      public JacksonRequest language(String langHeader)
      Overrides:
      language in class CommonRequest
    • body

      public JacksonRequest body(Object object)
      设置 JSON 请求体。
      Parameters:
      object - 要序列化的对象
      Returns:
      当前请求
      Throws:
      IllegalArgumentException - JSON 序列化失败时抛出
    • bodyEntity

      public JacksonRequest bodyEntity(org.apache.hc.core5.http.HttpEntity entity)
      Overrides:
      bodyEntity in class CommonRequest
    • resultAs

      public <T> T resultAs(Class<T> clazz) throws IOException
      将响应体解析为指定类型。
      Type Parameters:
      T - 结果类型
      Parameters:
      clazz - 目标类型
      Returns:
      解析结果
      Throws:
      IOException - 读取或解析失败
    • resultAs

      public <T> T resultAs(com.fasterxml.jackson.core.type.TypeReference<T> typeReference) throws IOException
      将响应体解析为指定泛型类型。
      Type Parameters:
      T - 结果类型
      Parameters:
      typeReference - 目标类型引用
      Returns:
      解析结果
      Throws:
      IOException - 读取或解析失败
    • resultAs

      public <T> T resultAs(Type type) throws IOException
      将响应体解析为指定反射类型。
      Type Parameters:
      T - 结果类型
      Parameters:
      type - 目标类型
      Returns:
      解析结果
      Throws:
      IOException - 读取或解析失败
    • asJsonNode

      public com.fasterxml.jackson.databind.JsonNode asJsonNode() throws IOException
      将响应体解析为 JSON 节点。
      Returns:
      JSON 节点
      Throws:
      IOException - 读取或解析失败
    • asJsonObject

      public com.fasterxml.jackson.databind.node.ObjectNode asJsonObject() throws IOException
      将响应体解析为 JSON 对象。
      Returns:
      JSON 对象节点
      Throws:
      IOException - 读取或解析失败
    • asJsonArray

      public com.fasterxml.jackson.databind.node.ArrayNode asJsonArray() throws IOException
      将响应体解析为 JSON 数组。
      Returns:
      JSON 数组节点
      Throws:
      IOException - 读取或解析失败