我在使用返回 JSON 字符串的 API 时遇到问题。当尝试将 API 的 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 时
字符串数据=“{“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
当我直接将 API 的响应转换为 Map 时出现错误
它接缝从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());
}
}