直接公開代碼
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
public class ObjectandObjetUtils<T> {
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
public T objetcMapperT(Object object,Class<T> valueType) throws Exception {
return OBJECT_MAPPER.readValue(OBJECT_MAPPER.writeValueAsString(object),valueType);
}
public T stringMapperT(String string,Class<T> valueType) throws Exception {
return OBJECT_MAPPER.readValue(string,valueType);
}
public List<T> objetcMapperTList(Object object,Class<T> valueType) throws Exception {
JavaType javaType =OBJECT_MAPPER.getTypeFactory().constructParametricType(List.class,valueType);
return OBJECT_MAPPER.readValue(OBJECT_MAPPER.writeValueAsString(object),javaType);
}
public List<T> stringMapperTList(String string,Class<T> valueType) throws Exception {
JavaType javaType =OBJECT_MAPPER.getTypeFactory().constructParametricType(List.class,valueType);
return OBJECT_MAPPER.readValue(string,javaType);
}
}