繁体   English   中英

如何使用 ObjectMapper 序列化/反序列化 LogEvent 对象?

[英]how to serialize/deserialize LogEvent Object using ObjectMapper?

嗨,我在客户端使用 LogEvent 对象来记录事件,我想使用 REST API 将其发送到服务器。 我正在将 LogEvent 对象转换为 json 字符串并通过 REST 将其作为有效负载发送。 在服务器端,我使用 Groovy,当我尝试执行 objectMapper.readValue() 时出现以下错误。

com.fasterxml.jackson.databind.JsonMappingException:找不到非具体集合类型[集合类型; 类 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. 

我无法使用 ObjectMapper 将 json 转换回对象。 谢谢你的帮助。

尝试 org.apache.logging.log4j.core.parser.JsonLogEventParser

JSONObject jobj = new JSONObject(jsonString);
LogEvent logEvent = new JsonLogEventParser().parseFrom(jsonString);

我在解析一些自定义 LogEvent 消息时遇到问题,然后我将 JSONLayout 与 setObjectMessageAsJsonObject(true) 选项一起使用,但如果它是默认 LogEvent,这对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM