日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【objectMapper实体转换异常】 com.fasterxml.jackson.databind.exc.MismatchedInputException

發(fā)布時間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【objectMapper实体转换异常】 com.fasterxml.jackson.databind.exc.MismatchedInputException 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

大家好,我是烤鴨:

? ? 采坑實錄,想把json數(shù)據(jù)直接轉(zhuǎn)成對象,其中有個屬性是list<T>:

?

異常 1

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token

這個是獲取到前端傳遞的參數(shù):

String str = "{\"vv\":\"1.6.6\",\"authCode\":\"1111\",\"appid\":\"2\",\"apptype\":\"2\",\"userPhone\":\"11111111111\",\"latitude\":\"39.92183\",\"phoneInfo\":\"[{\"name\":\"于11\",\"mobile\":\"13111119019\"}]\",\"time\":\"1540970481\",\"longitude\":\"116.28041\"}"; ObjectMapper objectMapper = new ObjectMapper(); UserInfoRequest userInfoRequest = objectMapper.readValue(str, UserInfoRequest.class); System.out.println(userInfoRequest);

報上面的錯。UserInfoRequest 就不貼了,屬性都是能對應(yīng)上的。其中有個List<phoneInfo>屬性。

查了一些資料。改成下面的代碼:

String str = "{\"vv\":\"1.6.6\",\"authCode\":\"1111\",\"appid\":\"2\",\"apptype\":\"2\",\"userPhone\":\"11111111111\",\"latitude\":\"39.92183\",\"phoneInfo\":\"[{\"name\":\"于11\",\"mobile\":\"13111119019\"}]\",\"time\":\"1540970481\",\"longitude\":\"116.28041\"}"; ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true) ; objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT); UserInfoRequest userInfoRequest = objectMapper.readValue(str, UserInfoRequest.class); System.out.println(userInfoRequest);

就報異常2的錯。

?

異常 2?

com.fasterxml.jackson.databind.exc.MismatchedInputException:

Cannot construct instance of `com.xxx.xxx` (although at least one Creator exists):

no String-argument constructor/factory method to deserialize from String value

因為自己測試的時候沒有這個問題,就仔細對比了測試的時候str和前端的傳值。

錯誤的:

String str = "{\"vv\":\"1.6.6\",\"authCode\":\"1111\",\"appid\":\"2\",\"apptype\":\"2\",\"userPhone\":\"11111111111\",\"latitude\":\"39.92183\",\"phoneInfo\":\"[{\"name\":\"于11\",\"mobile\":\"13111119019\"}]\",\"time\":\"1540970481\",\"longitude\":\"116.28041\"}";

正確的:

String str = "{\"vv\":\"1.6.6\",\"authCode\":\"1111\",\"appid\":\"2\",\"apptype\":\"2\",\"userPhone\":\"11111111111\",\"latitude\":\"39.92184\",\"phoneInfo\":[{\"name\":\"張三\",\"mobile\":\"15899996666\"},{\"name\":\"李四\",\"mobile\":\"138556688\"},{\"name\":\"王五\",\"mobile\":\"13855661118\"}],\"time\":\"1540969882\",\"longitude\":\"116.28033\"}";

?關(guān)鍵就在于?phoneInfo 后面跟的是字符串(""包括住的)還是 數(shù)組后者list對象。如果是字符串就報這個錯了。

\"phoneInfo\":\"[{\"name\":\"于11\",\"mobile\":\"1311111019\"}]\"

簡單一點的方式:? 把你的字符串去這個網(wǎng)站看一下是否是標(biāo)準(zhǔn)json。下面兩個圖一眼就看出問題了。

http://www.bejson.com/ ?

?

總結(jié):

知道問題在哪了,就好改了。因為獲取到前端參數(shù)的是v=1&x=2&b=3這種形式的。

解析參數(shù)的時候,放到map<String,String>中,最后把map轉(zhuǎn)成json字符串的格式。

沒有考慮到前端傳遞的是list這種的格式,判斷如果是 [ ]這種的話,就轉(zhuǎn)成jsonArray放到map。

map的格式是?<String,Object>。代碼如下:(pheader 是解密后的參數(shù),v=1&x=2&b=3這種形式)

public static String getParam(String cipher)throws Exception{Map<String, Object> params = new HashMap<String,Object>();//解析參數(shù)String pheader = "";pheader = AESUtil.decrypt(PpsConfig.getString("appkey"), cipher);String[] pArr = pheader.split("&");for (int i = 0; i < pArr.length; i++) {String[] tmp = pArr[i].split("=");if (tmp.length == 2) {//說明是數(shù)組if(tmp[1].startsWith("[")){JSONArray array = JSONArray.parseArray(tmp[1]);params.put(tmp[0], array);}else {params.put(tmp[0], tmp[1]);}}else{params.put(tmp[0], "");}}return JSONObject.toJSONString(params);}

?

總結(jié)

以上是生活随笔為你收集整理的【objectMapper实体转换异常】 com.fasterxml.jackson.databind.exc.MismatchedInputException的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。