尝试将 JSON 字符串响应从 API 转换为 Map 时出错<String, Object>

问题描述 投票:0回答:1

我在使用返回 JSON 字符串的 API 时遇到问题。当尝试将 API 的 JSON 字符串响应转换为 Map 时,它会抛出错误。但是,当我手动将相同的 JSON 字符串响应嵌入到我的代码中并执行到 Map 的转换时,它可以无缝运行。事实证明,理解这种行为对我来说非常具有挑战性


错误:

上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.BeanCreationException:创建名为“testService”的 bean 时出错:调用 init 方法失败;嵌套异常是 java.lang.RuntimeException: com.fasterxml.jackson.databind.exc.MismatchedInputException: 无法构造

java.util.LinkedHashMap
的实例(尽管至少存在一个 Creator):没有从 String 值反序列化的字符串参数构造函数/工厂方法('{') 在[来源:(字符串)“”{“DXX”:{“NX”:“XXDHX XXK XIMIXED”,“XXXX”:“X001654011000000087”,“NXme1”:“XINHX”,“XXhmId”:“16401”,“ XXhmNXme":"MXXXEXXXXD WXXXD","InXXXX":"1","XXXNXme":"XXXIVE","XXXXhgDX":"00000000","XpeXXXXMXinX":"XYX","DXXXXMXinX":"00000000","XvXXXg ":"I","XvXXXgNXme":"INDIVIDUXX","XuXXDiXXXXg":"N","XuXXDiXXXXXXDX":"00000000","XuXXDiXXXXXX":"00000000000000000000000000","XXkMXde":"P","DeXqMXde": "P","HighXedeemXmX":"0","HighXedeemPXX":"0","XXXXXhVXX":"0","XXdPXXEXXnd":"200","XXdPXXXXughX":"0","XXdPXXXnuX":"0 ","[截断 1399 个字符];行:1,列:1]


Json 字符串:

{
  "DXX": {
    "NX": "XXDHX XXK XIMIXED",
    "XXXX": "X001654011000000087",
    "NXme1": "XINHX",
    "XXhmId": "16401",
    "XXhmNXme": "MXXXEXXXXD WXXXD",
    "InXXXX": "1",
    "XXXNXme": "XXXIVE",
    "XXXXhgDX": "00000000",
    "XpeXXXXMXinX": "XYX",
    "DXXXXMXinX": "00000000",
    "XvXXXg": "I",
    "XvXXXgNXme": "INDIVIDUXX",
    "XuXXDiXXXXg": "N",
    "XuXXDiXXXXXDX": "00000000",
    "XuXXDiXXXXXX": "0000000000000000000000000",
    "XXkMXde": "P",
    "DeXqMXde": "P",
    "HighXedeemXmX": "0",
    "HighXedeemPXX": "0",
    "XXXXXhVXX": "0",
    "XXdPXXEXXnd": "70610",
    "XXdPXXXXughX": "0",
    "XXdPXXXnuX": "0",
    "XXdPXXXedeemed": "967",
    "XXdPXXXXXed": "0",
    "XXdPXXExpiXed": "0",
    "XXdPXXXdjIn": "10000",
    "XXdPXXXdjXuX": "0",
    "GXXXeXXd": "0",
    "XyXXXDXXXX": "10198",
    "XXdXpndEXXnd": "0",
    "YXdXpendEXXnd": "0",
    "XiXeXXDXXXX": "79643",
    "PXPXXXNXXXXd": "0",
    "XpenXXXedXnXXX": "79643",
    "XXmXMXhXXeq": "1",
    "XXmXDXyXXMXh": "15",
    "XXXXXmXDX": "20231215",
    "NexXXXmXDX": "20240115",
    "DXXpened": "20231204",
    "DiXXPkXPXX": "0",
    "DiXXPkXMXnVXXPXX": "0",
    "XuXXDiXXXXeq": "0",
    "XuXXDiXXDXyXXMXh": "0",
    "XXXXDiXXuXXeDX": "00000000",
    "XuXXDiXXXhXeXhXd": "0",
    "XuXXDiXXMXnVXX": "0",
    "WXiveMemXXee": "N",
    "XXXpnDX": "2023338",
    "XeXXXeXmX": "0",
    "XuXXNXd": "2",
    "PeXIXemNXd": "0",
    "PXXXXXeNXd": "7",
    "XnXXyXd": "IND",
    "XuXXXd": "356",
    "NXXMXhDXXXXXuX": "1",
    "MXnXhXyXXXhXXXkGXXup": [
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "20231215"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      },
      {
        "DiXXXmX": "0",
        "DiXXMXhDX": "00000000"
      }
    ],
    "XXXXXpenXniDXXe": "20241204",
    "XXdDiXXXXXhXXXk": "0",
    "XXdXpendXmX": "0"
  }
}

代码:

        HttpHeaders headers = new HttpHeaders();
        RestTemplate restTemplate = new RestTemplate();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Collections.singletonList(MediaType.ALL));
        headers.add("api_key","qwerqwersadfaf");
        headers.add("x-session-id","asdfasdf");
        headers.add("x-user-id","asdfasdfas");
        headers.add("x-message-id","asdfasdfas");

        String bodyTopost = "{\"Data\": {\"Account\": \"SAMPLEaccount\",\"Schm\": \"168401\"}}";

        HttpEntity<String> entity = new HttpEntity<String>(bodyTopost, headers);
        ParameterizedTypeReference<String> parameterizedTypeReference = new ParameterizedTypeReference<String>() {
        };
        String inputUrl = "https://api.testbank.co.in/cons/abcd/v1";

        try {
            ResponseEntity<String> result = restTemplate.exchange(inputUrl, HttpMethod.POST, entity, parameterizedTypeReference);
            if (result.getStatusCode().is2xxSuccessful()) {
                if (result.getBody() != null) {

                     String resultBody = result.getBody().replace("\\", "");

                    logger.info("Response value is {}", resultBody);

                    ObjectMapper mapper = new ObjectMapper();
                    Map<String, Object> map =  mapper.readValue(resultBody, Map.class);

                    return map;
                } else {
                    logger.info("Payload is null");
                    return null;
                }
            } else {
                logger.info("Failed to execute api");
                return null;
            }
        } catch (ResourceAccessException e) {
            logger.info("Resource Erro  {} ", e);
            return null;
        } catch (RestClientException e) {
            logger.info("Rest Error Erro  {} ", e);
            return null;

        } catch (JsonMappingException e) {
            throw new RuntimeException(e);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

尝试将 API 的 JSON 字符串响应转换为 Map 时,会抛出错误。但是,当我手动将相同的 JSON 字符串响应嵌入到我的代码中并执行转换为 Map 时,它可以无缝工作。

字符串数据=“{“DXX”:{“NX”:“XXDHX XXK XIMIXED”,“XXXX”:“X001654011000000087”,“NXme1”:“XINHX”,“XXhmId”:“16401”,“XXhmNXme”:“ MXXXEXXXXD WXXXD","InXXXX":"1","XXXNXme":"XXXIVE","XXXXhgDX":"00000000","XpeXXXXMXinX":"XYX","DXXXXMXinX":"00000000","XvXXXg":"I ","XvXXXgNXme":"INDIVIDUXX","XuXXDiXXXXg":"N","XuXXDiXXXXXXDX":"00000000","XuXXDiXXXXXX":"0000000000000000000000000","XXkMXde":"P","DeXqMXde":"P", "HighXedeemXmX":"0","HighXedeemPXX":"0","XXXXXhVXX":"0","XXdPXXEXXnd":"200","XXdPXXXXughX":"0","XXdPXXXnuX":"0","XXdPXXXedeemed ":"2","XXdPXXXXed":"0","XXdPXXExpiXed":"0","XXdPXXXdjIn":"10000","XXdPXXXdjXuX":"0","GXXXeXXd":"0","XyXXXDXXXX": “10198”,“XXdXpndEXXnd”:“0”,“XXdPXXXXXnd”:“70610”,“XXdPXXXXughX”:“0”,“XXdPXXXnuX”:“0”,“XXdPXXXedeemed”:“967”,“XXdPXXXXXed”:“0 ","XXdPXXExpiXed":"0","XXdPXXXdjIn":"10000","XXdPXXXdjXuX":"0","YXdXpendEXXnd":"0","GXXXeXXd":"0","XiXeXXDXXXX":"79643", “PXPXXXNXXXXd”:“0”,“XpenXXXedXnXXX”:“79643”,“XXmXMXhXXeq”:“1”,“XXmXDXyXXMXh”:“15”,“XXXXXmXDX”:“20231215”,“NexXXXmXDX”:“20240115”,“DXXpened” ":"20231204","DiXXPkXPXX":"0","DiXXPkXMXnVXXPXX":"0","XuXXDiXXXXeq":"0","XuXXDiXXDXyXXMXh":"0","XXXXDiXXuXXeDX":"00000000","XuXXDiXXXhXeXhXd": "0","XuXXDiXXMXnVXX":"0","WXiveMemXXee":"N","XXXpnDX":"2023338","XeXXXeXmX":"0","XuXXNXd":"2","PeXIXemNXd":"0 ","PXXXXXeNXd":"7","XnXXyXd":"IND","XuXXXd":"356","NXXMXhDXXXXXuX":"1","MXnXhXyXXXhXXXkGXXup":[{"DiXXXmX":"0","DiXXMXhDX ":"20231215"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0 ","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX ":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"} ,{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX":"00000000"},{"DiXXXmX":"0","DiXXMXhDX": "00000000"}],"XXXXXpenXniDXXe":"20241204","XXdDiXXXXXhXXXk":"0","XXdXpendXmX":"0"}}";

ObjectMapper 映射器 = new ObjectMapper(); Map map = mapper.readValue(data, Map.class);

当我直接将 API 的响应转换为 Map 时出现错误

java spring string objectmapper response-entity
1个回答
0
投票

它接缝从API返回的字符串有两个额外的引号,一个在字符串的开始,另一个在字符串的末尾,在堆栈跟踪中查看源属性:

[Source: (String)""{"DXX":{"NX":"XXDHX XXK ...

解决此问题的一个选项是在读取值之前删除多余的引号,如下所示:

int begin = resultBody.indexOf("{");
int end = resultBody.lastIndexOf("}") + 1;
resultBody= resultBody.substring(begin, end);

用于重现此错误的代码:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;

public class Main {

  public static void main(String[] args) throws JsonProcessingException {

    String resultBody="\"{\"DXX\":{\"NX\":\"XXDHX XXK XIMIXED\",\"XXXX\":\"X001654011000000087\",\"NXme1\":\"XINHX\",\"XXhmId\":\"16401\",\"XXhmNXme\":\"MXXXEXXXXD WXXXD\",\"InXXXX\":\"1\",\"XXXNXme\":\"XXXIVE\",\"XXXXhgDX\":\"00000000\",\"XpeXXXXMXinX\":\"XYX\",\"DXXXXMXinX\":\"00000000\",\"XvXXXg\":\"I\",\"XvXXXgNXme\":\"INDIVIDUXX\",\"XuXXDiXXXXg\":\"N\",\"XuXXDiXXXXXDX\":\"00000000\",\"XuXXDiXXXXXX\":\"0000000000000000000000000\",\"XXkMXde\":\"P\",\"DeXqMXde\":\"P\",\"HighXedeemXmX\":\"0\",\"HighXedeemPXX\":\"0\",\"XXXXXhVXX\":\"0\",\"XXdPXXEXXnd\":\"200\",\"XXdPXXXXughX\":\"0\",\"XXdPXXXnuX\":\"0\",\"XXdPXXXedeemed\":\"2\",\"XXdPXXXXXed\":\"0\",\"XXdPXXExpiXed\":\"0\",\"XXdPXXXdjIn\":\"10000\",\"XXdPXXXdjXuX\":\"0\",\"GXXXeXXd\":\"0\",\"XyXXXDXXXX\":\"10198\",\"XXdXpndEXXnd\":\"0\",\"XXdPXXEXXnd\":\"70610\",\"XXdPXXXXughX\":\"0\",\"XXdPXXXnuX\":\"0\",\"XXdPXXXedeemed\":\"967\",\"XXdPXXXXXed\":\"0\",\"XXdPXXExpiXed\":\"0\",\"XXdPXXXdjIn\":\"10000\",\"XXdPXXXdjXuX\":\"0\",\"YXdXpendEXXnd\":\"0\",\"GXXXeXXd\":\"0\",\"XiXeXXDXXXX\":\"79643\",\"PXPXXXNXXXXd\":\"0\",\"XpenXXXedXnXXX\":\"79643\",\"XXmXMXhXXeq\":\"1\",\"XXmXDXyXXMXh\":\"15\",\"XXXXXmXDX\":\"20231215\",\"NexXXXmXDX\":\"20240115\",\"DXXpened\":\"20231204\",\"DiXXPkXPXX\":\"0\",\"DiXXPkXMXnVXXPXX\":\"0\",\"XuXXDiXXXXeq\":\"0\",\"XuXXDiXXDXyXXMXh\":\"0\",\"XXXXDiXXuXXeDX\":\"00000000\",\"XuXXDiXXXhXeXhXd\":\"0\",\"XuXXDiXXMXnVXX\":\"0\",\"WXiveMemXXee\":\"N\",\"XXXpnDX\":\"2023338\",\"XeXXXeXmX\":\"0\",\"XuXXNXd\":\"2\",\"PeXIXemNXd\":\"0\",\"PXXXXXeNXd\":\"7\",\"XnXXyXd\":\"IND\",\"XuXXXd\":\"356\",\"NXXMXhDXXXXXuX\":\"1\",\"MXnXhXyXXXhXXXkGXXup\":[{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"20231215\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"},{\"DiXXXmX\":\"0\",\"DiXXMXhDX\":\"00000000\"}],\"XXXXXpenXniDXXe\":\"20241204\",\"XXdDiXXXXXhXXXk\":\"0\",\"XXdXpendXmX\":\"0\"}}\"";
//    int begin = resultBody.indexOf("{");
//    int end = resultBody.lastIndexOf("}") + 1;
//    resultBody= resultBody.substring(begin, end);
    Map<String, Object> map = new ObjectMapper().readValue(resultBody, Map.class);
    System.out.println(map.entrySet());
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.