在 spring 中插入数据字符串列表映射类型时如何修复空值

How to fix null when insert data string List Map type in spring

我在 Spring 中使用 rest 模板。我已经将数据解析为 json 的 rest 模板,如下所示:

 result = 
  [
  {
  "sendMessage": {
   "to": "84978965123",
   "telco": "04",
   "type": 1,
    "from": "SMS",
    "message": "String of message",
    "scheduled": "27-07-2021 13:00",
    "requestId": "",
    "useUnicode": 0,
    "ext": "{}"
  },
 },
{
"sendMessage": {
    "to": "84902032618",
    "telco": "01",
    "type": 1,
    "from": "SMS",
    "message": "String of message",
    "scheduled": "27-07-2021 13:00",
    "requestId": "",
    "useUnicode": 0,
     "ext": "{}"
   }
 }
]

结果是 json 的字符串,我想将所有数据插入数据库,然后是我的控制器来源:

try {

      if(result != null && !result.isEmpty()) {

      ObjectMapper mapper = new ObjectMapper();

      List<Map<String, Object>> listSendMessage = mapper.readValue(result, new TypeReference<List<Map<String, Object>>>(){});

        for(Map<String,Object> listElementIndex : listSendMessage) {


            List<Map<String, Object>> data =  (List<Map<String, Object>>) listElementIndex.get("mobileNoItems");

            for (int i = 0; i < data.size(); i++) {
                        
                System.out.println(data.get(i));
                        
                data.get(i).get("to");  
                data.get(i).get("telco");
                data.get(i).get("type");
                data.get(i).get("from");
                data.get(i).get("message");
                data.get(i).get("scheduled");
                data.get(i).get("requestId");
                data.get(i).get("useUnicode");
                data.get(i).get("ext");
            }
            smsService.insertNewScheduleSMS(data);

           }
         }
    else {
            logger.debug(SMSConstant.REQUEST_NO_BODY);
        }

    }
    catch(Exception e) {
            System.out.println(e.getMessage());
  }

当我 运行 和调试时发生异常为空,并且无法将字符串列表 json 插入数据库..

2021-07-28 18:49:53,221 DEBUG [com.myservice.SMSBrandingController] body        : {"ext":"{}","useUnicode":"0","scheduled":"28-07-2021 19:00","requestId":"","from":"SMS ","to":"0902032618","type":"1","message":"String of message"}
2021-07-28 18:49:53,221 DEBUG [com.myservice.SMSBrandingController] =======================log result end=================================================
null

(更新:)

Can not deserialize instance of java.util.LinkedHashMap out of VALUE_NUMBER_INT token
at [Source: [{"sendMessage":{"to":"84979123456","telco":"04","type":1,"from":"SMS","message":"String of message","scheduled":"28-07-2021 22:00","requestId":"","useUnicode":0,"ext":"{}"},"msgLength":162,"mtCount":2,"account":"Myaccount","errorCode":"000","errorMessage":"","referentId":"API_CSKH_myaccount_e293c777c0bc4852b6a20dc7d3f507b9"},{"sendMessage":{"to":"84902032618","telco":"01","type":1,"from":"SMS","message":"String of message","scheduled":"28-07-2021 22:00","requestId":"","useUnicode":0,"ext":"{}"},"msgLength":162,"mtCount":2,"account":"myaccount","errorCode":"000","errorMessage":"","referentId":"API_CSKH_myaccount_838ad57022d14fc8b8f01c302858d8da"}]; line: 1, column: 339] (through reference chain: java.util.ArrayList[0]->java.util.LinkedHashMap["msgLength"])

我该如何解决这个问题??

List<Map<String, Object>> data = (List<Map<String, Object>>) listElementIndex.get("mobileNoItems"); 行是问题所在,它是 "sendMessage" 而不是 "mobileNoItems"

List<Map<String, Object>> data =
    (List<Map<String, Object>>) listElementIndex.get("sendMessage");

证明(顺便说一句,我还更改了 TypeReference 以省略演员表):

import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Questions68560156 {

    String json = (""
            + "[                                              "
            + "     {                                         "
            + "         'sendMessage': {                      "
            + "             'to': '84942965123',              "
            + "             'telco': '04',                    "
            + "             'type': 1,                        "
            + "             'from': 'SMS',                    "
            + "             'message': 'String of message',   "
            + "             'scheduled': '27-07-2021 13:00',  "
            + "             'requestId': '',                  "
            + "             'useUnicode': 0,                  "
            + "             'ext': '{}'                       "
            + "         },                                    "
            + "         'msgLength' : 162                     "            
            + "     },                                        "
            + "     {                                         "
            + "         'sendMessage': {                      "
            + "             'to': '84902032618',              "
            + "             'telco': '01',                    "
            + "             'type': 1,                        "
            + "             'from': 'SMS',                    "
            + "             'message': 'String of message',   "
            + "             'scheduled': '27-07-2021 13:00',  "
            + "             'requestId': '',                  "
            + "             'useUnicode': 0,                  "
            + "             'ext': '{}'                       "
            + "         }                                     "
            + "     }                                         "
            + "]                                              ").replace('\'', '"');

    @Test
    public void test() throws JsonMappingException, JsonProcessingException {
        method(json);
    }

    void method(String result) throws JsonMappingException, JsonProcessingException {

        if (result != null && !result.isEmpty()) {
            ObjectMapper mapper = new ObjectMapper();
            List<Map<String, Object>> listSendMessage = mapper.readValue(result,
                    new TypeReference<List<Map<String, Object>>>() {
                    });

            for (Map<String, Object> listElementIndex : listSendMessage) {
                @SuppressWarnings("unchecked")
                Map<String, Object> attributes = (Map<String, Object>)listElementIndex.get("sendMessage");
                System.out.println(attributes);
                //...
            }
        } else {
            //                logger.debug(SMSConstant.REQUEST_NO_BODY);
        }

    }
}

打印:

{to=84942965123, telco=04, type=1, from=SMS, message=String of message, scheduled=27-07-2021 13:00, requestId=, useUnicode=0, ext={}}
{to=84902032618, telco=01, type=1, from=SMS, message=String of message, scheduled=27-07-2021 13:00, requestId=, useUnicode=0, ext={}}