我有一个带有Jersey的REST API,请求跟踪是这样的:
Información: 5 * Server out-bound response
5 < 200
5 < Content-Type: application/json
5 < X-Jersey-Trace-000: accept root resource classes: "/vales"
5 < X-Jersey-Trace-001: match path "/vales" -> "/application\.wadl(/.*)?", "/selacservicios(/.*)?", "/proveedores(/.*)?", "/vehiculos(/.*)?", "/empresas(/.*)?", "/vales(/.*)?", "/ok(/.*)?"
5 < X-Jersey-Trace-002: accept right hand path java.util.regex.Matcher[pattern=/vales(/.*)? region=0,6 lastmatch=/vales]: "/vales" -> "/vales" : ""
5 < X-Jersey-Trace-003: accept resource: "vales" -> @Path("/vales") com.grupogimeno.senda.siccagest.services.rest.ValeCompraResource@3f494991
5 < X-Jersey-Trace-004: match path "" -> ""
5 < X-Jersey-Trace-005: accept resource methods: "vales", GET -> com.grupogimeno.senda.siccagest.services.rest.ValeCompraResource@3f494991
5 < X-Jersey-Trace-006: matched resource method: public java.util.Map com.grupogimeno.senda.siccagest.services.rest.ValeCompraResource.getValesCompraDeUsuario(java.lang.String,java.lang.Integer)
5 < X-Jersey-Trace-007: matched message body writer: java.util.LinkedHashMap@b690aa46, "application/json" -> com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider@12b315ef
5 <
{"success":true,"data":[{"fGeneracion":1428579118957,"numValeCompra":"001562","abreviaturaEmpresa":"AGU. NUMANCIA","nombreProveedor":"MAJIMSA VALENCIA, S.A."},{"fGeneracion":1428579081230,"numValeCompra":"005647","abreviaturaEmpresa":"A.VINAROS-UTE","nombreProveedor":"EXCAV BABILONI, S.A."},{"fGeneracion":1428579081230,"numValeCompra":"005647","abreviaturaEmpresa":"A.VINAROS-UTE","nombreProveedor":"EXCAV BABILONI, S.A."}]}
有没有办法打印更漂亮的JSON响应?像这样:
{
"success": true,
"data": [
{
"fGeneracion": 1428579118957,
"numValeCompra": "001562",
"abreviaturaEmpresa": "AGU. NUMANCIA",
"nombreProveedor": "MAJIMSA VALENCIA, S.A."
},
{
"fGeneracion": 1428579081230,
"numValeCompra": "005647",
"abreviaturaEmpresa": "A.VINAROS-UTE",
"nombreProveedor": "EXCAV BABILONI, S.A."
},
{
"fGeneracion": 1428579081230,
"numValeCompra": "005647",
"abreviaturaEmpresa": "A.VINAROS-UTE",
"nombreProveedor": "EXCAV BABILONI, S.A."
}
]
}
这是web.xml
中的过滤器:
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
</init-param>
感谢。
答案 0 :(得分:1)
您可以将ObjectMapper
配置为configure(SerializationFeature.INDENT_OUTPUT, true);
。您可以在ContextResolver
中将其配置为seen here。
请注意,这将格式化实际响应,而不仅仅是日志记录。日志记录似乎只打印响应&#34;原样&#34;,所以我不知道如何没有另一个(除了编写自己的记录器)
答案 1 :(得分:0)
使用com.google.gson.Gson
-
然后代码将是 -
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(JsonObject);
LOGGER.info("JSON prettier response: "+json);