com.faster xml .jackson.databind.ex c .MismatchedInputException: невозможно создать экземпляр `java .util.LinkedHashMap` - PullRequest
2 голосов
/ 04 апреля 2020

Из одного проекта я отправляю данные в другой проект. У меня есть свои данные в LinkedHashMap.
Когда я отправляю, я конвертирую карту в Json: objectMapper.writeValueAsString(visitToInsurer) Когда я пытаюсь преобразовать json значение в карту, я делаю:

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
}

А потом я получил ошибку: 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 ('{"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":[]}')

ОБНОВЛЕНИЕ: Спасибо всем. Проблема была в моем коде, когда я отправляю сообщение от производителя подписчику. Я пытаюсь отправить json строку в поле для объекта, а не в поле для json строки.

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

Так что в зависимости это не работает, потому что думаю, что это уже Map (и я отправляю Строка, в которой находится карта)

1 Ответ

1 голос
/ 06 апреля 2020
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);

/ * Если нам нужен объект LinkedHashMap

LinkedHashMap personMap = (LinkedHashMap) 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}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...