我将这个JSON字符串从Kafka反序列化到java对象时出错。
{"type":"CASA","batchList":["CVA348856650375401ABC","BNM346846350035548DEF"]}
字符串
我的反序列化代码是这样的:
ObjectMapper objectMapper = new ObjectMapper();
FooDTO fooDto = objectMapper.readValue(payload.value(), new TypeReference<FooDTO>() {});
型
我还尝试了以下方法
//objectMapper.readValue(payload.value(), FooDTO.class);
//objectMapper.readValue(objectMapper.writeValueAsString(object), FooDTO.class));
//objectMapper.readValue(objectMapper.writeValueAsString(object), new TypeReference<FooDTO>()));
型
什么都不管用然后我认为它与空格和下一行有关,因为当我在代码中手动添加json字符串时,即:String jsonString = "{\"type\":\"CASA\",\"batchList\":[\"CVA348856650375401ABC\",\"BNM346846350035548DEF\"]}";
反序列化代码可以工作。
下一行或 "\n” 未删除。我尝试了以下方法:
String jsonStringReplace = payload.value().replace("\n", "");
String jsonStringTrim = payload.value().trim();
String jsonStringStrip = payload.value().strip();
String jsonStringRegex = payload.value().replaceAll("\\r|\\n", "");
型
都是这样的:
raw: "{\n \"type\": \"CASA\",\n \"batchList\": [\n \"CVA348856650375401ABC\",\n \"BNM346846350035548DEF\"\n ]\n}"
jsonStringRepAll: "{\n \"type\": \"CASA\",\n \"batchList\": [\n \"CVA348856650375401ABC\",\n \"BNM346846350035548DEF\"\n ]\n}"
jsonStringTrim: "{\n \"type\": \"CASA\",\n \"batchList\": [\n \"CVA348856650375401ABC\",\n \"BNM346846350035548DEF\"\n ]\n}"
jsonStringStrip: "{\n \"type\": \"CASA\",\n \"batchList\": [\n \"CVA348856650375401ABC\",\n \"BNM346846350035548DEF\"\n ]\n}"
jsonStringRegex: "{\n \"type\": \"CASA\",\n \"batchList\": [\n \"CVA348856650375401ABC\",\n \"BNM346846350035548DEF\"\n ]\n}"
型
我总是一遍又一遍地犯同样的错误:
Cannot construct instance of `sample.ms.dto.FooDTO` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{
"type": "CASA",
"batchList": [
"CVA348856650375401ABC",
"BNM346846350035548DEF"
]
}')
型
下面是代码:
@Incoming(KafkaConstants.KAFKA_TOPIC_REQUEST)
@Acknowledgment(Acknowledgment.Strategy.POST_PROCESSING)
@Blocking
@Retry
public Response consumeRequest(Record<String, String> payload) {
log.info("+++++ START CONSUMING REQUEST +++++");
log.debug("{} : {}", payload.key(), payload.value());
try {
if (Optional.ofNullable(payload.value()).isPresent()) {
ObjectMapper objectMapper = new ObjectMapper();
FooDTO fooDto = objectMapper.readValue(payload.value(), new TypeReference<FooDTO>()
{});
...
}
}
型
下面的代码是DTO:
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Valid
public class FooDTO {
//@NotNull
private String batchKey;
//@NotNull
private String type;
//@NotNull
private List<String> batchList;
}
型
1条答案
按热度按时间xtfmy6hx1#
首先,在代码中不要为每个请求示例化一个新的
ObjectMapper
。将其作为具有您的方法consumeRequest
的类的成员并重用它。此外,为了让你的代码更简单,你可以使用一个3d party实用程序JsonUtils
,它是ObjectMapper
类的一个薄 Package 器,可以在一行中完成你想要做的事情:字符串
您的String
"{\"type\":\"CASA\",\"batchList\":[\"CVA348856650375401ABC\",\"BNM346846350035548DEF\"]}"
应该可以正常工作。JsonUtils类附带由我编写和维护的MgntUtils开源库。下面是JsonUtils Javadoc
,该库可以作为Maven artifact或GitHub获得(包括源代码和Javadoc)