简体   繁体   English

如何轻松地将JSON转换为字符串以进行记录

[英]how to easily convert a JSON to a string for logging purposes

I will try to explain what I'm trying to accomplish as clear as I can. 我将尽力澄清我要完成的工作。

I'm writing the back end of a web application, that uses REST APIs for extracting data that is used for reports in the client side. 我正在写一个Web应用程序的后端,该Web应用程序使用REST API提取用于客户端报告的数据。 The framework I'm writing on uses Codehaus jackson for parsing the requests from JSON to data objects (data beans). 我正在编写的框架使用Codehaus jackson来解析从JSON到数据对象(数据bean)的请求。 I have a bunch of APIs of this sort, 10-15. 我有一堆此类API,例如10-15。 each of them gets a different request object from the client side (though some inheritance do exist). 它们每个都从客户端获得一个不同的请求对象(尽管确实存在一些继承)。 what I want to do is add logging (using log4j) for each of these APIs so that when I enter each of the methods the request data object will be logged. 我想要做的是为每个这些API添加日志记录(使用log4j),以便当我输入每个方法时,将记录请求数据对象。 the simple solution would be to implement a toString() method for each of these data objects, but I want to avoid going over all of these data objects and see if there's a solution similiar to the way that jackson parses the JSON into an object. 最简单的解决方案是为每个数据对象实现toString()方法,但我想避免遍历所有这些数据对象,并查看是否存在类似于杰克逊将JSON解析为对象的方法的解决方案。 Ie have the object converted back to its textual format in order to put it into the log. 即,将对象转换回其文本格式,以便将其放入日志。

I assume there's so easy way to do so. 我认为有这么简单的方法。

This is an example of a REST API and its data bean: 这是REST API及其数据Bean的示例:

@POST
@Path("/path123/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response getSomeData(@Context HttpServletRequest httpServletRequest, DataBeanExample bean){
    DataBeanExample resultBean;
    //DO SOME STUFF , for example take the bean, extract parameters from it and call some other api.
    return Response.ok(resultBean, MediaType.APPLICATION_JSON).build();

}


@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "DataBeanExample")

public class DataBeanExample{

@XmlElement(name = "timeFrame", required = true)
private String timeFrame = "";
@XmlElement(name = "timeFrom",required = true)
private long timeFrom;
@XmlElement(name = "timeTo",required = true)
private long timeTo;

public String getTimeFrame() {
    return timeFrame;
}

public void setTimeFrame(String timeFrame) {
    this.timeFrame = timeFrame;
}

public long getTimeTo() {
    return timeTo;
}

public void setTimeTo(long timeTo) {
    this.timeTo = timeTo;
}

public long getTimeFrom() {
    return timeFrom;
}

public void setTimeFrom(long timeFrom) {
    this.timeFrom = timeFrom;
}

} }

In the example, what I want to do is at the beginning of "getSomeData" take the object bean and have it logged. 在示例中,我想做的是在“ getSomeData”的开头获取对象bean并将其记录下来。

Probably this might work 可能这可能有效

new ObjectMapper().writeValue(System.out, dataBeanExampleInstance)

or writeValueAsString writeValueAsString

Do you know about: 您是否知道:

JSON.stringify JSON.stringify

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

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