com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `java.util.LinkedHashMap`

huangapple go评论86阅读模式
英文:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `java.util.LinkedHashMap`

问题

<br />
从一个项目中,我将数据发送到另一个项目。
我将我的数据存储在LinkedHashMap中。<br />
当我发送数据时,我将Map转换为Json:

objectMapper.writeValueAsString(visitToInsurer)

<br /> <br />
当我尝试将json值转换回map时,我执行以下操作:

T getJsonMessage(byte[] body) throws IOException {
  return objectMapper.readValue(body, resolveGenericType());
}   
Class&lt;T&gt; resolveGenericType() {
   Class&lt;?&gt;[] tClass = GenericTypeResolver.resolveTypeArguments(getClass(), SomeClass.class);
   return (Class&lt;T&gt;) tClass[0];//这里我得到了Map的接口
}

<br />

然后我遇到了一个错误:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of java.util.LinkedHashMap (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value

<br /> <br />

('{"id":1,"id2":2,"bDate":"2020-04-04T16:22:45.032087","eDate":"9999-12-31T00:00","cDate":"2020-04-04T16:22:45.032087","use":"1","act":true,"in":0,"vDt":null,"vDt2":null,"dCheck":null,"vFlag":true,"nFlag":false,"vFlag2":false,"nFlag2":false,"uFlag":true,"rFlag":false,"dId":1,"it":1,"rId":1,"rCode":"1","iNum":"1","iAb":"OOO\\"TEST-1 \\"TEST\\"","dId3":2,"dId4":2,"dName":"Test","vId5":null,"vId6":null,"pDt7":null,"pDt8":null,"vDt9":null,"vDt10":null,"wCnt":null,"dId8":null,"dId9":null,"dName10":null,"dName11":"Test","cReason":null,"vd12":null,"vId13":null,"vNum14":null,"vDate15":null,"act":null,"rLst":[{"id":1,"id2":2,"bDate":"2020-04-04T16:22:45.236460","eDate":"9999-12-31T00:00","cDate":"2020-04-04T16:22:45.236460","user":"1","vId":1,"vId2":1,"dId":2,"dId2":2,"dName":"TTT","pDt":null,"pDt2":null,"vDt":null,"vDt2":null,"cBegin":"2020-05-04T00:00:00","cEnd":"2020-05-05T00:00:00","cNum":"111","amount":0,"amount2":0,"prem":0,"f":0,"pen":0,"dId5":1,"dName6":"Test","aFlag":false,"rId":11,"rCode":"11","cNum":0,"dId7":1,"pId8":null,"pId9":null,"act":null}],"vCard":{"id":1,"id2":1,"bDate":"2020-04-04T16:22:45.032087","eDate":"9999-12-31T00:00","cDate":"2020-04-04T16:22:45.032087","user":"1","act":true,"invalid":0,"rId":1,"rCode":"1","iName":"TEST \\"TEST \\"TEST\\"","iName2":"TEST\\"TEST \\"TEST\\"","rNum":"1","in5":"1","k77":"1","og77":"11","rBegin":"2009-09-28T00:00:00","inDate":"2009-09-28T00:00:00","outDate":null,"phone":null,"localAddress":"TEST","email":null,"iStatus":"State","sCode":"1","iCode":"1","oCode":"1","cBegin5":"1900-01-01T00:00:00","pName":null,"pName2":null,"pReg":null,"action":null,"vList":[{"id":1,"id2":"1","cCode":"1","vId":1,"vId2":1,"bDate":"2020-04-04T16:22:45.032087","eDate":"9999-12-31T00:00","cDate":"2020-04-04T16:22:45.032087","user":"1","actual":true,"rId":1,"rCode":"1","came":"ТTEST","cPhone":null,"cP":"TEST","action":null}],"vList2":[]},"vList3":[{"id":1,"id2":1,"bDate":"2020-04-04T16:22:45.095369","eDate":"9999-12-31T00:00","cDate":"2020-04-04T16:22:45.095369","user":"1","actual":true,"vId":1,"vId":1,"tNum":"1","surname":"TEST","name":"TEST","pat":"TEST","pName2":"TEST","phone":null,"kId":1,"kId2":1,"vChecks":"TEST","nFlag":false,"vFlag":true,"nFlag2":false,"vFlag2":false,"action":null}],"vList5":[]}'))

UPDATE:
谢谢大家。问题出在我发送消息从生产者到订阅者的代码中。我试图将JSON字符串发送到对象字段中,而不是JSON字符串字段中。

public class ProducerMsg {
    private String body;
    private Object objectBody;
}

因此,在依赖中它不起作用,因为它认为它已经是Map(而我发送的是包含Map的字符串)。

英文:

<br />
From one project I send data to another project.
I have my data in LinkedHashMap.<br />
When I send, I convert map in Json:
objectMapper.writeValueAsString(visitToInsurer)<br /> <br />
When I try to convert json value to map, I do:

T getJsonMessage(byte[] body) throws IOException {
return objectMapper.readValue(body, resolveGenericType());
}   
Class&lt;T&gt; resolveGenericType() {
Class&lt;?&gt;[] tClass = GenericTypeResolver.resolveTypeArguments(getClass(), SomeClass.class);
return (Class&lt;T&gt;) tClass[0];//here i get interface of Map
}

<br />

And then I got an error:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of java.util.LinkedHashMap (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value <br /> <br />
(&#39;{&quot;id&quot;:1,&quot;id2&quot;:2,&quot;bDate&quot;:&quot;2020-04-04T16:22:45.032087&quot;,&quot;eDate&quot;:&quot;9999-12-31T00:00&quot;,&quot;cDate&quot;:&quot;2020-04-04T16:22:45.032087&quot;,&quot;use&quot;:&quot;1&quot;,&quot;act&quot;:true,&quot;in&quot;:0,&quot;vDt&quot;:null,&quot;vDt2&quot;:null,&quot;dCheck&quot;:null,&quot;vFlag&quot;:true,&quot;nFlag&quot;:false,&quot;vFlag2&quot;:false,&quot;nFlag2&quot;:false,&quot;uFlag&quot;:true,&quot;rFlag&quot;:false,&quot;dId&quot;:1,&quot;it&quot;:1,&quot;rId&quot;:1,&quot;rCode&quot;:&quot;1&quot;,&quot;iNum&quot;:&quot;1&quot;,&quot;iAb&quot;:&quot;OOO\&quot;TEST-1 \&quot;TEST\&quot;&quot;,&quot;dId3&quot;:2,&quot;dId4&quot;:2,&quot;dName&quot;:&quot;Test&quot;,&quot;vId5&quot;:null,&quot;vId6&quot;:null,&quot;pDt7&quot;:null,&quot;pDt8&quot;:null,&quot;vDt9&quot;:null,&quot;vDt10&quot;:null,&quot;wCnt&quot;:null,&quot;dId8&quot;:null,&quot;dId9&quot;:null,&quot;dName10&quot;:null,&quot;dName11&quot;:&quot;Test&quot;,&quot;cReason&quot;:null,&quot;vd12&quot;:null,&quot;vId13&quot;:null,&quot;vNum14&quot;:null,&quot;vDate15&quot;:null,&quot;act&quot;:null,&quot;rLst&quot;:[{&quot;id&quot;:1,&quot;id2&quot;:2,&quot;bDate&quot;:&quot;2020-04-04T16:22:45.236460&quot;,&quot;eDate&quot;:&quot;9999-12-31T00:00&quot;,&quot;cDate&quot;:&quot;2020-04-04T16:22:45.236460&quot;,&quot;user&quot;:&quot;1&quot;,&quot;vId&quot;:1,&quot;vId2&quot;:1,&quot;dId&quot;:2,&quot;dId2&quot;:2,&quot;dName&quot;:&quot;TTT&quot;,&quot;pDt&quot;:null,&quot;pDt2&quot;:null,&quot;vDt&quot;:null,&quot;vDt2&quot;:null,&quot;cBegin&quot;:&quot;2020-05-04T00:00:00&quot;,&quot;cEnd&quot;:&quot;2020-05-05T00:00:00&quot;,&quot;cNum&quot;:&quot;111&quot;,&quot;amount&quot;:0,&quot;amount2&quot;:0,&quot;prem&quot;:0,&quot;f&quot;:0,&quot;pen&quot;:0,&quot;dId5&quot;:1,&quot;dName6&quot;:&quot;Test&quot;,&quot;aFlag&quot;:false,&quot;rId&quot;:11,&quot;rCode&quot;:&quot;11&quot;,&quot;cNum&quot;:0,&quot;dId7&quot;:1,&quot;pId8&quot;:null,&quot;pId9&quot;:null,&quot;act&quot;:null}],&quot;vCard&quot;:{&quot;id&quot;:1,&quot;id2&quot;:1,&quot;bDate&quot;:&quot;2020-04-04T16:22:45.032087&quot;,&quot;eDate&quot;:&quot;9999-12-31T00:00&quot;,&quot;cDate&quot;:&quot;2020-04-04T16:22:45.032087&quot;,&quot;user&quot;:&quot;1&quot;,&quot;act&quot;:true,&quot;invalid&quot;:0,&quot;rId&quot;:1,&quot;rCode&quot;:&quot;1&quot;,&quot;iName&quot;:&quot;TEST \&quot;TEST \&quot;TEST\&quot;&quot;,&quot;iName2&quot;:&quot;TEST\&quot;TEST \&quot;TEST\&quot;&quot;,&quot;rNum&quot;:&quot;1&quot;,&quot;in5&quot;:&quot;1&quot;,&quot;k77&quot;:&quot;1&quot;,&quot;og77&quot;:&quot;11&quot;,&quot;rBegin&quot;:&quot;2009-09-28T00:00:00&quot;,&quot;inDate&quot;:&quot;2009-09-28T00:00:00&quot;,&quot;outDate&quot;:null,&quot;phone&quot;:null,&quot;localAddress&quot;:&quot;TEST&quot;,&quot;email&quot;:null,&quot;iStatus&quot;:&quot;State&quot;,&quot;sCode&quot;:&quot;1&quot;,&quot;iCode&quot;:&quot;1&quot;,&quot;oCode&quot;:&quot;1&quot;,&quot;cBegin5&quot;:&quot;1900-01-01T00:00:00&quot;,&quot;pName&quot;:null,&quot;pName2&quot;:null,&quot;pReg&quot;:null,&quot;action&quot;:null,&quot;vList&quot;:[{&quot;id&quot;:1,&quot;id2&quot;:&quot;1&quot;,&quot;cCode&quot;:&quot;1&quot;,&quot;vId&quot;:1,&quot;vId2&quot;:1,&quot;bDate&quot;:&quot;2020-04-04T16:22:45.032087&quot;,&quot;eDate&quot;:&quot;9999-12-31T00:00&quot;,&quot;cDate&quot;:&quot;2020-04-04T16:22:45.032087&quot;,&quot;user&quot;:&quot;1&quot;,&quot;actual&quot;:true,&quot;rId&quot;:1,&quot;rCode&quot;:&quot;1&quot;,&quot;came&quot;:&quot;ТTEST&quot;,&quot;cPhone&quot;:null,&quot;cP&quot;:&quot;TEST&quot;,&quot;action&quot;:null}],&quot;vList2&quot;:[]},&quot;vList3&quot;:[{&quot;id&quot;:1,&quot;id2&quot;:1,&quot;bDate&quot;:&quot;2020-04-04T16:22:45.095369&quot;,&quot;eDate&quot;:&quot;9999-12-31T00:00&quot;,&quot;cDate&quot;:&quot;2020-04-04T16:22:45.095369&quot;,&quot;user&quot;:&quot;1&quot;,&quot;actual&quot;:true,&quot;vId&quot;:1,&quot;vId&quot;:1,&quot;tNum&quot;:&quot;1&quot;,&quot;surname&quot;:&quot;TEST&quot;,&quot;name&quot;:&quot;TEST&quot;,&quot;pat&quot;:&quot;TEST&quot;,&quot;pName2&quot;:&quot;TEST&quot;,&quot;phone&quot;:null,&quot;kId&quot;:1,&quot;kId2&quot;:1,&quot;vChecks&quot;:&quot;TEST&quot;,&quot;nFlag&quot;:false,&quot;vFlag&quot;:true,&quot;nFlag2&quot;:false,&quot;vFlag2&quot;:false,&quot;action&quot;:null}],&quot;vList5&quot;:[]}&#39;)

UPDATE:
Thank you all. Problem was in my code where I send message from producer to subscriber. I try to send json string in field for object, not in field for json string.

public class ProducerMsg {
private String body;
private Object objectBody;
}

So in dependency it doesnt't work because think that it is already Map(and I send String in which is Map)

答案1

得分: 1

请参考以下代码:

public class JsonToMapDemo {
    public static void main(String[] args) {

        String json = "{\"name\":\"Akshay\",\"age\":\"23\"}";
        convertJsonToMap(json);

    }

    private static void convertJsonToMap(String jsonString) {
        try {

            Map<String, Object> personMap =  new ObjectMapper().readValue(jsonString, Map.class);

            /*
            If we need LinkedHashMap Object
            
            LinkedHashMap<String, Object> personMap =  (LinkedHashMap<String, Object>) new ObjectMapper().readValue(jsonString, Map.class);
            */

            System.out.println(personMap);

        } 
        catch (JsonGenerationException e)
        { 
            e.printStackTrace(); 

        } 
        catch (JsonMappingException e)
        { 
            e.printStackTrace();

        } catch (JsonProcessingException e) {

            e.printStackTrace();
        }
    }
}

输出结果:

{name=Akshay, age=23}
英文:
Please refer below code
public class JsonToMapDemo {
public static void main(String[] args) {
String json = &quot;{\&quot;name\&quot;:\&quot;Akshay\&quot;,\&quot;age\&quot;:\&quot;23\&quot;}&quot;;
convertJsonToMap(json);
}
private static void convertJsonToMap(String jsonString) {
try {
Map&lt;String, Object&gt; personMap =  new ObjectMapper().readValue(jsonString, Map.class);

/*
If we need LinkedHashMap Object

LinkedHashMap<String, Object> personMap = (LinkedHashMap<String, Object>) new ObjectMapper().readValue(jsonString, Map.class);
*/

		System.out.println(personMap);
} 
catch (JsonGenerationException e)
{ 
e.printStackTrace(); 
} 
catch (JsonMappingException e)
{ 
e.printStackTrace();
} catch (JsonProcessingException e) {
e.printStackTrace();
}

}

code output
{name=Akshay, age=23}

huangapple
  • 本文由 发表于 2020年4月5日 00:58:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/61031589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定