Java 在泛型函数中是否有方法将List对象强制转换为确切的List类?

Java 在泛型函数中是否有方法将List对象强制转换为确切的List类?,java,spring-boot,generics,objectmapper,Java,Spring Boot,Generics,Objectmapper,我在一个Spring Boot项目中编写代码,有很多API都有不同的请求参数,所以我试图用mapper将一个请求参数写入一个list对象,然后将其转换为一个类,如下面的代码所示 public static <D> List<D> convertStringListToObject(String string) { if (string == null) return null; try { return ob

我在一个Spring Boot项目中编写代码,有很多API都有不同的请求参数,所以我试图用mapper将一个请求参数写入一个list对象,然后将其转换为一个类,如下面的代码所示

    public static <D> List<D> convertStringListToObject(String string) {
        if (string == null) return null;
        try {
            return objectMapper.readValue(string, new TypeReference<>() {
            });
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
FilterBlockRequestDto类

package com.levitate.projectbe.dto.filter;

import com.levitate.projectbe.dto.common.PopularFiltersDto;
import com.levitate.projectbe.dto.common.TotalBudgetDto;
import lombok.*;

import java.util.List;

@Getter
@Setter
@Builder
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class FilterBlockRequestDto {
    Integer locationId;
    Integer projectId;
    String totalBudget;
    List<TotalBudgetDto> totalBudgetList;
    // The string was pass in Request param
    String popularFilters;
    List<PopularFiltersDto> popularFiltersList;
    Integer viewRating;
    Integer numberOfBed;
}

package com.levitate.projectbe.dto.filter;
导入com.levitate.projectbe.dto.common.PopularFiltersDto;
导入com.levitate.projectbe.dto.common.totalBudgetTo;
进口龙目。*;
导入java.util.List;
@吸气剂
@塞特
@建筑商
@托斯特林
@AllArgsConstructor
@诺尔格构装师
公共类筛选器BlockRequestdTo{
整数位置ID;
整数投影;
字符串总预算;
总预算清单;
//字符串已传入请求参数
字符串过滤器;
列表popularFiltersList;
整数收视率;
整数床;
}

您还必须传递要反序列化字符串的类型

我的方法是这样的:

public static <T> T convertStringListToObject(String string, Class<T> clazz) {
    if (string == null) {
        return null;
    }
    try {
       return objectMapper.readValue(string, clazz);
    } catch (JsonProcessingException e) {
       e.printStackTrace();
    }
    return null;
}
public static T convertStringListToObject(字符串,类clazz){
if(字符串==null){
返回null;
}
试一试{
返回objectMapper.readValue(字符串,clazz);
}捕获(JsonProcessingException e){
e、 printStackTrace();
}
返回null;
}
然后按如下方式使用此方法:

List<Model> models = 
    Arrays.asList(Mapper.convertStringListToObject(stringList, Model[].class));
列出模型=
asList(Mapper.convertStringListToObject(stringList,Model[].class));

一种方法是接受类型引用作为参数,以便调用者可以提供目标类,并且由于
TypeReference
是一个子类,因此在运行时可以使用泛型类型信息

    public static <D> List<D> convertStringListToObject(String string, TypeReference<List<D>> typeReference) {
        if (string == null) return null;
        try {
            return objectMapper.readValue(string, typeReference);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
公共静态列表转换器StringListToObject(字符串、类型引用、类型引用){
if(string==null)返回null;
试一试{
返回objectMapper.readValue(字符串,类型引用);
}捕获(JsonProcessingException e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}

请显示您是如何调用它的。噢:
newtypereference(){}
不起作用。您需要将
TypeReference
作为参数传入。@下面是调用它的方法
filterBlockRequestDto.setPopularFiltersList(ApiUtil.convertStringListToObject(filterBlockRequestDto.getPopularFilters())。我已经试着像你之前说的那样添加,但是编译器和IDE一直在抱怨,所以我遵循建议,最终结果是上面的函数。你能给我一个例子,里面的传递类型是D吗?它仍然被转换成一个对象,而不是我想要的那个类
    public static <D> List<D> convertStringListToObject(String string, TypeReference<List<D>> typeReference) {
        if (string == null) return null;
        try {
            return objectMapper.readValue(string, typeReference);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }