您好,登錄后才能下訂單哦!
小編給大家分享一下怎么解決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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。