1

I have a REST API with Jersey and the requests traces is like this:

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."}]}

Is there a way to print the JSON response prettier? Like this:

{
    "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."
        }
    ]
}

This is the filter in 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>

Thanks.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Héctor
  • 24,444
  • 35
  • 132
  • 243

2 Answers2

1

You can configure the ObjectMapper to configure(SerializationFeature.INDENT_OUTPUT, true);. You can configure it in a ContextResolver, as seen here.

Note though that this will format the actual response, and not just the logging. It seems the logging just prints the response "as-is", so I don't know how you would have one without the other (except by writing your own logger)

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

Use com.google.gson.Gson -

The code will then be -

Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(JsonObject); 
LOGGER.info("JSON prettier response: "+json);
sjain
  • 23,126
  • 28
  • 107
  • 185