[英]how to serialize/deserialize LogEvent Object using ObjectMapper?
Hi I have LogEvent Object used on client side to Log events and I want to send it to to server using REST API.嗨,我在客户端使用 LogEvent 对象来记录事件,我想使用 REST API 将其发送到服务器。 I'm converting LogEvent Object to json string and sending it as payload through REST.
我正在将 LogEvent 对象转换为 json 字符串并通过 REST 将其作为有效负载发送。 On Server side I'm using Groovy and when I try to do objectMapper.readValue() I'm getting following error.
在服务器端,我使用 Groovy,当我尝试执行 objectMapper.readValue() 时出现以下错误。
com.fasterxml.jackson.databind.JsonMappingException: Can not find a deserializer for non-concrete Collection type [collection type; com.fasterxml.jackson.databind.JsonMappingException:找不到非具体集合类型[集合类型; class org.apache.logging.log4j.ThreadContext$ContextStack, contains [simple type, class java.lang.String]]
类 org.apache.logging.log4j.ThreadContext$ContextStack,包含 [简单类型,类 java.lang.String]]
/// Client Side code.
private final List<LogEvent> eventQueue = new LinkedList<>();
List<LogEvent> logToSend;
eventsToSend = new ArrayList<>(eventQueue);
String jsonLogStream = new String();
ObjectMapper mapperObj = new ObjectMapper();
try{
jsonLogStream = mapperObj.writeValueAsString(logToSend);
}catch (IOException e){
e.printStackTrace();
}
closeableHttpResponse = communicationManager.executeHttpPost(uri, Collections.emptyMap(), new ByteArrayInputStream(jsonLogStream.getBytes()), false);
//// In Groovy
//
String logstream = request.getJSON().toString()
//here I'm getting LogEvents converted to json
LogEvent[] events = mapper.readValue(logstream, LogEvent[].class )
// mapper.readvalue giving error mentioned.
I'm not able to convert json back to object using ObjectMapper.我无法使用 ObjectMapper 将 json 转换回对象。 Thanks for your help.
谢谢你的帮助。
Try org.apache.logging.log4j.core.parser.JsonLogEventParser尝试 org.apache.logging.log4j.core.parser.JsonLogEventParser
JSONObject jobj = new JSONObject(jsonString);
LogEvent logEvent = new JsonLogEventParser().parseFrom(jsonString);
I have a problem to parse some custom LogEvent message then I use JSONLayout with setObjectMessageAsJsonObject(true) option, but if it's a default LogEvent, this worked for me.我在解析一些自定义 LogEvent 消息时遇到问题,然后我将 JSONLayout 与 setObjectMessageAsJsonObject(true) 选项一起使用,但如果它是默认 LogEvent,这对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.