title: How to print a REST JSON request/response in the logs?
body: <p>I'm calling a JSON REST service and in the returned response, I have escape characters. </p>
  
  <p>I want to avoid the escape characters (to have them interpreted), and pretty print a JSON response. I cannot use GSON because of some constraints. I've tried the pretty print option, but that doesn't seem to work. </p>
  
  <p>All of this has to be done in JAVA.</p>
  
  <p>For eg.</p>
  
  <pre><code>String originalString = "{\n    "name":"xyz",\n    "age":"23"\n}"
  </code></pre>
  
  <p>is available as a String. This has to be converted to </p>
  
  <pre><code>String newString = {"name":"xyz","age":"23"}
  </code></pre>
  
  <p>If possible, I should be able to pretty print it in the logs with proper indentation - something like,</p>
  
  <pre><code>{
      "name":"xyz",
      "age":"23"
  }
  </code></pre>
  
  <p>I'm getting the original string in the logs from a REST JSON service response. The new string can be printed somehow in the logs (StringBuilder or some other way). </p>
  
  <p>EDIT: Going up one level, the reason this question was asked is to print the request/responses in the logs. I've used <a href="https://stackoverflow.com/questions/10210645/http-servlet-request-lose-params-from-post-body-after-read-it-once">this</a> as an input while doing it for the request. However, after logging the request, I'm facing an issue logging the response. The problem reads, </p>
  
  <pre><code>Request processing failed; nested exception is java.lang.IllegalStateException: SRVE0209E: Writer already obtained
  </code></pre>
  
  <p>Has anyone faced a similar issue? The problem I believe is being able to cache the response like it is being done for the request in the example link above.</p>