1

I am following a spring boot tutorial here:

Spring boot tutorial for consuming a rest service

The tutorial works fine, but is there a way I can print the raw JSON before it is unmarshalled (via the jackson JSON processing library) for logging purposes?

njk2015
  • 543
  • 9
  • 20

1 Answers1

3

The quickest way to do this is to set the log level to DEBUG for the HTTP input buffer : simply add logging.level.org.apache.coyote.http11.Http11InputBuffer = DEBUG to your src/main/resources/application.properties file. You should then see something similar to the following lines in your log messages:

2017-02-28 17:35:03.554 DEBUG 856 --- [nio-8443-exec-5] o.a.coyote.http11.Http11InputBuffer      : Received [POST /tasks HTTP/1.1
Host: localhost:8443
Authorization: Basic ****************
User-Agent: curl/7.51.0
Accept: */*
content-type:application/json
Content-Length: 17

{ "foo" : "bar" }]

Also check this SO post where I offered several options regarding tinkering with the raw JSON content with home made controller methods.

Community
  • 1
  • 1
Marc Tarin
  • 3,109
  • 17
  • 49
  • Thanks! But if I also wanted to grab it as a string and print to the console? – njk2015 Feb 28 '17 at 16:49
  • Apart from the home made solution I offered - which forces you to override every PUT/POST endpoints, I assume it can be done using web filters, but I've never done that so I can't tell you for sure. Check this [thread](http://stackoverflow.com/questions/10457963/spring-rest-service-retrieving-json-from-request), or this [one](http://stackoverflow.com/questions/8933054/how-to-read-and-copy-the-http-servlet-response-output-stream-content-for-logging) - though it may be a bit overwhelming if you're just starting to discover Spring Boot :) – Marc Tarin Feb 28 '17 at 17:08
  • I'll give the suggested solution a try first, before delving into the other posts. Thanks! – njk2015 Feb 28 '17 at 17:13
  • I was looking to only output the JSON and not the other header information. Any way to omit the other information? – njk2015 Feb 28 '17 at 17:18