在fromMap执行下面我想获得泛型类型的运行时类型T
使用TypeTools
但我坚持一个NullPointerException
的TypeResolver.resolveRawArguments(MapperImpl.class, getClass());
。
我怎样才能做到这一点?
Mapper.java:
import java.util.Map;
public interface Mapper<T extends Object, S extends Map<String, Object>> {
public S toMap(T obj);
public T fromMap(S map);
}
MapperImpl.java
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import net.jodah.typetools.TypeResolver;
public class MapperImpl<T extends Object, S extends Map<String, Object>>
implements Mapper<T, S> {
public S toMap(T obj) {
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> map = objectMapper.convertValue(obj, Map.class);
return (S) map;
}
public T fromMap(S map) {
ObjectMapper objectMapper = new ObjectMapper();
Class<?>[] ts = TypeResolver.resolveRawArguments(MapperImpl.class, getClass());
Class<T> classT = (Class<T>) ts[0];
T obj = objectMapper.convertValue(map, classT);
return obj;
}