简体   繁体   English

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

[英]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.

相关问题 如何使用ObjectMapper从空JSON文件反序列化为Java Object - How to deserialize from empty JSON file to Java Object using ObjectMapper 如何使用 ObjectMapper 在没有默认构造函数的情况下反/序列化不可变对象? - How to de/serialize an immutable object without default constructor using ObjectMapper? 如何序列化/反序列化arrayList(Object) - how to serialize/deserialize arrayList(Object) 如何序列化/反序列化 object 到 Map - How to serialize/deserialize object to Map 使用Jackson ObjectMapper进行序列化 - Serialize using Jackson ObjectMapper 如何使用杰克逊对象映射器反序列化 - How to deserialize with jackson objectmapper 如何使用Java从Google地理编码序列化和反序列化JSON对象 - How to serialize and deserialize a JSON object from Google geocode using Java 如何使用 Jackson 和包装器 object 反序列化/序列化字节数组 - how to deserialize / serialize byte array using Jackson and wrapper object 使用杰克逊(ObjectMapper)如何将对象序列化为json并忽略除我注释为@JsonProperty的字段以外的所有其他字段? - Using Jackson (ObjectMapper) how serialize object to json and ignore all fields except the ones I annotate as @JsonProperty? 使用 jackson 序列化和反序列化具有抽象字段的对象 - Serialize and deserialize an object with an abstract field using jackson
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM