【问题标题】:how to serialize/deserialize LogEvent Object using ObjectMapper?如何使用 ObjectMapper 序列化/反序列化 LogEvent 对象?
【发布时间】:2019-06-03 17:58:35
【问题描述】:

您好,我在客户端使用了 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 转换回对象。 谢谢你的帮助。

【问题讨论】:

    标签: java json rest log4j objectmapper


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      • 2019-10-25
      • 1970-01-01
      • 2014-12-10
      相关资源
      最近更新 更多