编写一个方法,方法签名如下
public <T> T getBean(String key, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException {
在外部调用这个方法,传参如下
list=xxx.getBean(key, List.class);
但是代码出现下面的警告
Type safety: The expression of type List needs unchecked conversion to conform to List<DataMap>
出去使用添加注解@SuppressWarnings("unchecked")还有其他的解决办法吗?
不知道你用的是哪个json库,但是机制都类似(名字也类似)
比如jackson有一个
TypeReference<T>
用来处理这种问题,写起来是这样: