当前位置: 首页 > news >正文

TypeReference 泛型的使用场景及具体使用流程

简介

在 Java 中,泛型类型在运行时会被擦除。这意味着当我们使用泛型时,运行时无法直接获取到泛型的具体类型信息。例如,我们无法直接通过 Class 对象来获取一个泛型类型的类型参数。这在某些情况下可能会导致问题,特别是在我们需要对泛型类型进行反射操作时。

TypeReference 是 Jackson 库提供的一个类,它可以帮助我们解决这个问题。通过 TypeReference,我们可以在运行时保留泛型的类型信息,从而能够正确地处理复杂的泛型类型。

本文将详细介绍 TypeReference 泛型的使用场景及具体使用流程,并通过一个实际例子展示如何在自定义 HttpClient 中使用 TypeReference

使用场景

处理复杂泛型的 JSON 反序列化

在使用 Jackson 库进行 JSON 反序列化时,如果目标类型是一个复杂的泛型类型,直接使用 Class 对象可能会导致类型擦除问题,从而无法正确地反序列化。例如,我们有一个泛型类 Map<String, List<String>>,如果我们直接使用 Class<Map<String, List<String>>> 来进行反序列化,会因为类型擦除而无法正确地获取到具体的泛型类型参数。

通过 TypeReference,我们可以正确地指定泛型类型参数,从而能够正确地反序列化复杂的泛型类型。

处理复杂泛型的反射操作

在进行反射操作时,我们可能需要获取泛型类型的类型参数。由于类型擦除,直接使用 Class 对象无法获取到这些类型参数。而 TypeReference 可以帮助我们保留这些类型参数,从而能够正确地进行反射操作。

具体使用流程

引入 Jackson 依赖

在使用 TypeReference 之前,我们需要先引入 Jackson 的依赖。如果你使用的是 Maven 项目,可以在 pom.xml 文件中添加以下依赖:

<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.13.3</version>
</dependency>

定义 TypeReference 的子类

为了使用 TypeReference,我们需要定义一个 TypeReference 的子类。这个子类需要继承自 TypeReference,并且指定具体的泛型类型。例如,如果我们需要处理 Map<String, List<String>> 类型,可以这样定义:

new TypeReference<Map<String, List<String>>>() {};

使用 TypeReference 进行 JSON 反序列化

在使用 Jackson 进行 JSON 反序列化时,我们可以使用 TypeReference 来指定具体的泛型类型。例如:

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;import java.io.IOException;
import java.util.List;
import java.util.Map;public class TypeReferenceExample {public static void main(String[] args) throws IOException {String json = "{\"key1\":[\"value1\",\"value2\"],\"key2\":[\"value3\",\"value4\"]}";ObjectMapper objectMapper = new ObjectMapper();TypeReference<Map<String, List<String>>> typeReference = new TypeReference<Map<String, List<String>>>() {};Map<String, List<String>> map = objectMapper.readValue(json, typeReference);System.out.println(map);}
}

在上面的代码中,我们定义了一个 TypeReference<Map<String, List<String>>> 的子类,并将其传递给 ObjectMapperreadValue 方法。这样,Jackson 就可以正确地反序列化 JSON 字符串到 Map<String, List<String>> 类型。

使用 TypeReference 进行反射操作

在进行反射操作时,我们可以通过 TypeReference 获取泛型类型的类型参数。例如:

import com.fasterxml.jackson.core.type.TypeReference;import java.lang.reflect.Type;public class TypeReferenceReflectionExample {public static void main(String[] args) {TypeReference<Map<String, List<String>>> typeReference = new TypeReference<Map<String, List<String>>>() {};Type type = typeReference.getType();System.out.println(type);}
}

在上面的代码中,我们通过 TypeReferencegetType 方法获取了泛型类型的 Type 对象。这个 Type 对象包含了泛型类型的类型参数信息,我们可以使用它来进行反射操作。

实际例子:HttpClient 通用类实现

在实际开发中,我们经常需要与外部服务进行 HTTP 通信。为了提高代码的复用性和可维护性,我们通常会自定义一个 HttpClient 类,用于封装 HTTP 请求和响应处理。由于返回值类型不固定,我们可以使用 TypeReference 来实现一个通用的 HttpClient 类。

定义 HttpClient 类

我们定义一个通用的 HttpClient 类,用于发送 HTTP 请求并处理响应。这个类将使用 TypeReference 来支持返回值类型不固定的场景。

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;import java.io.IOException;public class HttpClient {private static final ObjectMapper objectMapper = new ObjectMapper();private static final CloseableHttpClient httpClient = HttpClients.createDefault();public static <T> T get(String url, TypeReference<T> typeReference) throws IOException {HttpGet httpGet = new HttpGet(url);try (CloseableHttpResponse response = httpClient.execute(httpGet)) {String jsonResponse = EntityUtils.toString(response.getEntity());return objectMapper.readValue(jsonResponse, typeReference);}}
}

使用 HttpClient 类

我们可以使用 HttpClient 类来发送 HTTP 请求,并指定返回值的类型。例如,假设我们有一个外部服务返回一个 Map<String, List<String>> 类型的 JSON 数据,我们可以这样使用 HttpClient 类:

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;import java.util.List;
import java.util.Map;public class HttpClientExample {public static void main(String[] args) {try {String url = "https://api.example.com/data";Map<String, List<String>> result = HttpClient.get(url, new CustomTypeReference().mapReference);System.out.println(result);} catch (IOException e) {e.printStackTrace();}}
}

自定义 TypeReference

import java.util.List;
import java.util.Map;import com.fasterxml.jackson.core.type.TypeReference;/*** CustomTypeReference 自定义TypeReference** @author xxx* @since 2025/7/24*/
public class CustomTypeReference {public TypeReference<Map<String, List<String>>> mapReference =new TypeReference<Map<String, List<String>>>() {};
}

在上面的代码中,我们定义了一个 TypeReference<Map<String, List<String>>> 的子类,并将其传递给 HttpClient.get 方法。这样,HttpClient 类就可以正确地反序列化返回的 JSON 数据到 Map<String, List<String>> 类型。


至此,本次分享到此结束啦!!!

http://www.dtcms.com/a/346512.html

相关文章:

  • C语言宏用法
  • 单例模式与线程池
  • Ollama 本地部署 Qwen2.5-7b
  • 神经网络|(十一)概率论基础知识-贝叶斯公式和全概率公式
  • 概率论基础教程第六章 随机变量的联合分布(二)
  • C++哈希表:unordered_map与unordered_set全解析
  • day12(练习题)
  • Java微服务架构设计模式详解
  • Baumer高防护相机如何通过YoloV8深度学习模型实现社交距离的检测识别(python)
  • 【Protues仿真】基于AT89C52单片机的超声波测距
  • 关于微服务下的不同服务之间配置不能通用的问题
  • `strdup` 字符串复制函数
  • 自控笔记 控制框图简化+梅森公式复习
  • Spring Boot 实时广播消息
  • 分布式蜜罐系统的部署安装
  • TypeScript 类中提供的 public、private 和 protected 修饰符的作用理解
  • C++程序嵌入python脚本调试
  • 搜索算法在实际场景中的应用
  • 系统思考:突破复杂困境
  • Python 十进制转二进制
  • 使用隧道(Tunnel)连接PostgreSQL数据库(解决防火墙问题)(含Java实现代码)
  • GIS实现综合应急管理平台及应用
  • Linux系统 --- 指令
  • CMake link_directories()详细介绍与使用指南
  • 算法题(188):团伙
  • 【0基础PS】蒙版与剪贴蒙版详解
  • `strchr` 字符串查找函数
  • 信息结构统一论:物理世界与人类感知、认知及符号系统的桥梁
  • 嵌入式-时钟树和时钟树编程-Day20
  • 大宗商品、股票指数及ETF期权五档买卖分时tick级历史交易数据深度解析