英文:
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<T> resolveGenericType() {
Class<?>[] tClass = GenericTypeResolver.resolveTypeArguments(getClass(), SomeClass.class);
return (Class<T>) 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<T> resolveGenericType() {
Class<?>[] tClass = GenericTypeResolver.resolveTypeArguments(getClass(), SomeClass.class);
return (Class<T>) 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 />
('{"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:
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 = "{\"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();
}
}
code output
{name=Akshay, age=23}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论