Spring MVC allows for logging of request and response body to allow for easier debugging and verification of message content. This is required for my project for auditing purposes, the log messages MUST contain the full request and response body.
Using Spring Web Reactive and Webclient, how to log request and response body without hex values?
The required format is RAW HTTP request, Ex.
PUT /api/v1/target/{id}
HTTP/1.1
Host: https://testsite.com:8080
Authorization: Bearer myToken
Content-Type: application/json
{
"test": "test
}
Currently, other answers only provide a rough hex+msg output using Reactor Netty debug logging OR output only HTTP headers for request. Ex. of current implementation:
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 48 54 54 50 2f 31 2e 31 20 32 30 30 20 4f 4b 0d |HTTP/1.1 200 OK.|
|00000010| 0a 53 65 72 76 65 72 3a 20 6e 67 69 6e 78 2f 31 |.Server: nginx/1|
|00000020| 2e 31 36 2e 31 0d 0a 44 61 74 65 3a 20 4d 6f 6e |.16.1..Date: Mon|
|00000030| 2c 20 30 35 20 4f 63 74 20 32 30 32 30 20 31 33 |, 05 Oct 2020 13|
|00000040| 3a 35 39 3a 33 36 20 47 4d 54 0d 0a 43 6f 6e 74 |:59:36 GMT..Cont|
|00000050| 65 6e 74 2d 54 79 70 65 3a 20 61 70 70 6c 69 63 |ent-Type: applic|
|00000060| 61 74 69 6f 6e 2f 6a 73 6f 6e 3b 20 63 68 61 72 |ation/json; char|
|00000070| 73 65 74 3d 75 74 66 2d 38 0d 0a 43 6f 6e 74 65 |set=utf-8..Conte|
|00000080| 6e 74 2d 4c 65 6e 67 74 68 3a 20 31 30 37 38 0d |nt-Length: 1078.|
|00000090| 0a 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 6b 65 65 |.Connection: kee|
|000000a0| 70 2d 61 6c 69 76 65 0d 0a 58 2d 50 6f 77 65 72 |p-alive..----|
.....
.....
This is much more difficult to read and for auditors to copy-paste.
Is there a way to alter this format? How does one simply print the request body.
I am OK with breaking the non-blocking recommendation within Spring Webflux. RestTemplate is listed as targeted for deprecation which means Webclient will be used for blocking operations. Auditing is more important than performance for this project.
Answering here after testing all the options in the included comments.
To pull in the relevant dependencies
Create the method that will execute during the webclient flow. Used in the definition of the webclient:
Define the webclient using the following method (this includes an example of defining the webclient as a bean to be injected as needed):