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 文件中添加以下依赖:

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>> 类型,可以这样定义:

java 复制代码
new TypeReference<Map<String, List<String>>>() {};

使用 TypeReference 进行 JSON 反序列化

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

java 复制代码
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 获取泛型类型的类型参数。例如:

java 复制代码
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 来支持返回值类型不固定的场景。

java 复制代码
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 类:

java 复制代码
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

java 复制代码
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>> 类型。


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

相关推荐
txwtech18 小时前
第4篇 vs2019+QT调用SDK连接海康相机显示图片
开发语言·数码相机·qt
没有bug.的程序员18 小时前
MySQL 在金融系统中的应用:强一致性与高可用架构实战
java·mysql·金融·架构·高可用·强一致性
王嘉俊92518 小时前
Flask 入门:轻量级 Python Web 框架的快速上手
开发语言·前端·后端·python·flask·入门
sibylyue19 小时前
IDEA AI Agent
java·ide·intellij-idea
做运维的阿瑞19 小时前
Python 面向对象编程深度指南
开发语言·数据结构·后端·python
芒途之士19 小时前
okHttp 解决文件上传中文名错误 Unexpected char
java
木木子999919 小时前
Python的typing模块:类型提示 (Type Hinting)
开发语言·windows·python
她说人狗殊途19 小时前
Spring IoC容器加载过程 vs Bean生命周期对应关系图
java·开发语言·rpc
std787919 小时前
超越编辑器:IntelliJ IDEA,如何成为Java开发的智慧引擎
java·编辑器·intellij-idea
Zz_waiting.19 小时前
Spring 统一功能处理 - 拦截器与适配器
java·spring·拦截器·适配器·dispatcher