小编给大家分享一下怎么解决ObjectMapper.convertValue() 遇到的一些问题,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
源代码:
public <T> T convertValue(Object fromValue, TypeReference<?> toValueTypeRef) throws IllegalArgumentException { return (T) _convert(fromValue, _typeFactory.constructType(toValueTypeRef)); }
例子:
List<SObject> sObjects = new ObjectMapper().convertValue(map.get("list"), new TypeReference<List<SObject>>() { });
微服务中从其他服务获取过来的对象,如果从Object强转为自定义的类型会报错,利用ObjectMapper转换。
ObjectMapper mapper = new ObjectMapper(); DefaultResponse defaultResponse = proxy.getData(); List<Resource> resources = (<Resource>) defaultResponse.getData(); //这里的场景是:data是一个Object类型的,但是它其实是一个List<Resouce>,想把List中的每个对象分别转成可用的对象 for (int i = 0; i < serviceDateResources.size(); i++) { Resource resource = mapper.convertValue(resources.get(i), Resource.class); //经过这步处理,resource就是可用的类型了,如果不转化会报错 }
在转换过程中有些属性被设置为空,这样就不需要转化
在需要转化的实体类商添加如下注解
@JsonInclude(Include.NON_NULL) @JsonInclude(Include.Include.ALWAYS) 默认 @JsonInclude(Include.NON_DEFAULT) 属性为默认值不序列化 @JsonInclude(Include.NON_EMPTY) 属性为 空(“”) 或者为 NULL 都不序列化 @JsonInclude(Include.NON_NULL) 属性为NULL 不序列化
ObjectMapper mapper = new ObjectMapper();
User user=new User(); String userJson=mapper.writeValueAsString(user);
Map map=new HashMap(); String json=mapper.writeValueAsString(map);
Student[] stuArr = {student1, student3}; String jsonfromArr = mapper.writeValueAsString(stuArr);
String expected = "{\"name\":\"Test\"}"; User user = mapper.readValue(expected, User.class);
String expected = "{\"name\":\"Test\"}"; Map userMap = mapper.readValue(expected, Map.class);
String expected="[{\"a\":12},{\"b\":23},{\"name\":\"Ryan\"}]"; CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, User.class); List<User> userList = mapper.readValue(expected, listType);
String expected="[{\"a\":12},{\"b\":23},{\"name\":\"Ryan\"}]"; CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, Map.class); List<Map<String,Object>> userMapList = mapper.readValue(expected, listType);
String expected = "[{\"name\":\"Ryan\"},{\"name\":\"Test\"},{\"name\":\"Leslie\"}]"; ArrayList arrayList = mapper.readValue(expected, ArrayList.class);
ObjectMapper objectMapper = new ObjectMapper(); //遇到date按照这种格式转换 SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); objectMapper.setDateFormat(fmt); String preference = "{name:'侯勇'}"; //json字符串转map Map<String, String> preferenceMap = new HashMap<String, String>(); preferenceMap = objectMapper.readValue(preference, preferenceMap.getClass()); //map转json字符串 String result=objectMapper.writeValueAsString(preferenceMap);
List<Map<String,String>> returnList=new ArrayList<Map<String,String>>(); List<Menu> menuList=menuDAOImpl.findByParentId(parentId); ObjectMapper mapper = new ObjectMapper(); //用jackson将bean转换为map returnList=mapper.convertValue(menuList,new TypeReference<List<Map<String, String>>>(){});
报错信息如下:
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;
看完了这篇文章,相信你对“怎么解决ObjectMapper.convertValue() 遇到的一些问题”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。