当我在lambda函数中使用new TypeReference<T>() {}
并且T
来自外部函数泛型参数时,它会抛出null异常。但是当这个语句直接写在函数中时就可以了。
- Java版本:8
- Jackson版本:2.12.5
有最小的可运行代码示例。
这将得到java.lang.IllegalArgumentException: Unrecognized Type: [null]
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
String test = "[\"test\"]";
List<String> strings = getObjectConfig(test, new ArrayList<>());
System.out.println(strings);
}
public static <T> T getObjectConfig(String key, T defaultValue) {
return getCommon(
key,
v -> {
if (Objects.isNull(v)) {
return defaultValue;
} else {
try {
return new ObjectMapper().readValue(v, new TypeReference<T>() {});
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
);
}
private static <T> T getCommon(String key, Function<String, T> transFunc) {
try {
String value = key;
return transFunc.apply(value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
但这没关系。
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
String test = "[\"test\"]";
List<String> strings = getObjectConfig(test, new ArrayList<>());
System.out.println(strings);
}
private static <T> T getObjectConfig(String key, T defaultValue) throws JsonProcessingException {
return new ObjectMapper().readValue(key, new TypeReference<T>() {});
}
}
1条答案
按热度按时间vsnjm48y1#
经过几次测试,我能够用Java 11重现你的bug。请注意,一切都与Java 17正常工作。
首先,我发现this post是关于一个类似的问题。
然后,为了解决这个问题,我专门通过了我遵循的类:
我没有使用
TypeReference
,而是使用了Class
。