gpt4 book ai didi

java - Jackson 字符串反序列化失败

转载 作者:行者123 更新时间:2023-12-02 03:16:18 28 4
gpt4 key购买 nike

我们的系统利用 Jackson (Java 11) 反序列化来自外部源的对象。当我们收到有效负载时,它是字符串 (UTF-8) 格式。反序列化失败并出现异常 (MismatchedInputException)

无法构造“对象”的实例(尽管至少存在一个创建者):没有字符串参数构造函数/工厂方法来从字符串值反序列化('{

堆栈技术:

  • Java 11
  • jackson 2.9.8
  • Google Cloud(平台)

我们最终使用了多个replaceAll语句,只是为了让有效负载处于允许字符串转换为jackson中的对象的状态。代码真的很臭...

I'm not sure if this helps but the payload is pulled from Google PubSub Subscription. So I have a message receiver listens on the subscription. I was originally using Springs JacksonPubSubMessageConverter but was throwing the exception above. I rolled my own, adding the replaceAll(..) below and now that seems to have fixed the problem.

public class ABCMessageReceiver implements MessageReceiver {
private PubsubMessageConverter converter;
public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
Person person = converter.fromPubSubMessage(message, Person.class);
...
}
}
payload = "{\n  \"general\": {\n    \"aggregatorId\": \"111111111111\",\n    \"communityId\": \"30303030\",\n    \"dateStamp\": \"2019-07-09\",\n    \"comments\": \"Testing E2E1\"\n  },\n  \"prospect\": {\n    \"firstName\": \"Joe\",\n    \"lastName\": \"Smith\",\n    \"nickName\": \"\",\n    \"email\": \"joe.smith@sample.com\",\n    \"gender\": \"MALE\",\n    \"maritalStatus\": \"SINGLE\",\n    \"dateOfBirth\": \"1956-06-15\",\n    \"veteranStatus\": \"NOTAVETERAN\",\n    \"address\": {\n      \"address1\": \"100 Acme Street \",\n      \"address2\": \"Suite 2300\",\n      \"city\": \"Acme City\",\n      \"state\": \"WI\",\n      \"zip\": \"53214\"\n    },\n    \"phones\": [\n      {\n        \"number\": \"4145551212\",\n        \"type\": \"WORK\"\n      }\n    ],\n    \"financial\": {\n      \"budgetAmount\": 2639,\n      \"budgetFrequency\": \"MONTHLY\",\n      \"medicaid\": true,\n      \"medicare\": true,\n      \"ltcPolicy\": false,\n      \"homeowner\": true,\n      \"vaAid\": false\n    },\n    \"prospectNeeds\": {\n      \"desiredCareLevel\": \"AL\"\n    }\n  },\n  \"tour\": {\n    \"date\": \"2019-07-14T17:00:00.000Z\",\n    \"notes\": \"Testing tour notes 1\"\n  }\n}"
payload = payload.replaceAll("\\\\\"", "\"");
payload = payload.replaceAll("\\\\n", "");
payload = payload.substring(1);
payload = payload.substring(0, payload.length()-1);

我不喜欢开销字符串替换,但它现在似乎有效,直到我得到需要支持的另一个字符串排列。

最佳答案

我要感谢@Andreas 和其他提供评论的人。关于编码,一开始没有连接,我必须第二天再回来处理。是的,如果生产者提交格式正确的 JSON,我会很高兴。这是我为使其正常工作所做的,不是最好的解决方案,但它确实有效。我扩展了 JacksonPubSubMessageConverter.java 并创建了一个新的未编码版本。

UnencodedJacksonPubSubMessageConverter.java

...
public <T> T fromPubSubMessage(PubsubMessage message, Class<T> payloadType) {
try {
String payload = message.getData().toStringUtf8();
payload = removeQuotesIfNecessary(unescapePayload(payload));
return (T) this.objectMapper.readerFor(payloadType).readValue(payload);
}
catch (IOException ex) {
throw new PubSubMessageConversionException("JSON deserialization of an object of type " + payloadType.getName() + " failed.", ex);
}
}

private String removeQuotesIfNecessary(String payload) {
if (payload != null && payload.startsWith("\"") && payload.endsWith("\"")) {
return payload.substring(1).substring(0, payload.length()-2);
}
return payload;
}

private String unescapePayload(String payload) {
if (payload != null)
return StringEscapeUtils.unescapeJson(payload);
return payload;
}

关于java - Jackson 字符串反序列化失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56959720/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com