解决ObjectMapper.convertValue() 遇到的一些问题

这篇文章主要介绍了解决ObjectMapper.convertValue() 遇到的一些问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

代码

public T convertValue(Object fromValue, TypeReference> tovalueTypeRef) throws IllegalArgumentException { return (T) _convert(fromValue, _typeFactory.constructType(tovalueTypeRef)); }

方法用于用jackson将bean转换为map

例子:

List sObjects = new ObjectMapper().convertValue(map.get("list"), new TypeReference>() { });

微服务中从其他服务获取过来的对象,如果从Object强转为自定义的类型会报错,利用ObjectMapper转换。

ObjectMapper mapper = new ObjectMapper(); DefaultResponse defaultResponse = proxy.getData(); List resources = () defaultResponse.getData(); //这里的场景是:data是一个Object类型的,但是它其实是一个List,想把List中的每个对象分别转成可用的对象 for (int i = 0; i

在转换过程中有些属性被设置为空,这样就不需要转化

处理方法

在需要转化的实体类添加如下注解

@JsonInclude(Include.NON_NULL) @JsonInclude(Include.Include.ALWAYS) 认 @JsonInclude(Include.NON_DEFAULT) 属性认值不序列化 @JsonInclude(Include.NON_EMPTY) 属性为 空(“”) 或者为 NULL 都不序列化 @JsonInclude(Include.NON_NULL) 属性为NULL 不序列化

jackson objectMapper json字符串、对象bean、map、数组list互相转换常用的方法列举:

ObjectMapper mapper = new ObjectMapper();

1.对象转json字符串

User user=new User(); String userjson=mapper.writeValueAsstring(user);

2.Map转json字符串

Map map=new HashMap(); String json=mapper.writeValueAsstring(map);

3.数组list转json字符串

Student[] stuArr = {student1, student3}; String jsonfromArr = mapper.writeValueAsstring(stuArr);

4.json字符串转对象

String expected = "{"name":"Test"}"; User user = mapper.readValue(expected, User.class);

5.json字符串转Map

String expected = "{"name":"Test"}"; Map userMap = mapper.readValue(expected, Map.class);

6.json字符串转对象数组List

String expected="[{"a":12},{"b":23},{"name":"Ryan"}]"; CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, User.class); List userList = mapper.readValue(expected, listType);

7.json字符串转Map数组List>

String expected="[{"a":12},{"b":23},{"name":"Ryan"}]"; CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, Map.class); List> userMapList = mapper.readValue(expected, listType);

8.jackson认将对象转换为LinkedHashMap:

String expected = "[{"name":"Ryan"},{"name":"Test"},{"name":"Leslie"}]"; ArrayList arrayList = mapper.readValue(expected, ArrayList.class);

9.json字符串与list或map互转的方法

ObjectMapper objectMapper = new ObjectMapper(); //遇到date按照这种格式转换 SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); objectMapper.setDateFormat(fmt); String preference = "{name:'侯勇'}"; //json字符串转map Map preferenceMap = new HashMap(); preferenceMap = objectMapper.readValue(preference, preferenceMap.getClass()); //map转json字符串 String result=objectMapper.writeValueAsstring(preferenceMap);

10.bean转换为map

List> returnList=new ArrayList>(); List menuList=menuDAOImpl.findByParentId(parentId); ObjectMapper mapper = new ObjectMapper(); //用jackson将bean转换为map returnList=mapper.convertValue(menuList,new TypeReference>>(){});

objectMapper.convertValue() 报错

报错信息如下:com.fasterxml.jackson.databind.exc.InvalidDeFinitionException: Cannot construct instance of java.time.LocalDateTime (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: UNKNowN; line: -1, column: -1] (through reference chain: net.too1.tplus.user.user.entity.User[“createTime”])根据以上报错得知, 是java.time.LocalDateTime类型的原因. ObjectMapper 不能对LocalDateTime 序列化. 加上以下注解即可解决@JsonDeserialize(using = LocalDateTimeDeserializer.class) @JsonSerialize(using = LocalDateTimeSerializer.class)@ApiModelProperty(value = "创建时间") @JsonDeserialize(using = LocalDateTimeDeserializer.class) @JsonSerialize(using = LocalDateTimeSerializer.class) private LocalDateTime createTime;

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...