Request Body Caching in Spring Boot Application
Usecase:
When an exception is thrown from an application. The exception will be directed to ExceptionControllerAdvice for performing any custom logic like sending notifications to the error queue or for sending alarms.
The message payload of the notification should contain the request body, this will help the support team to understand for which request this exception has been logged into SQS or SNS, etc.
To enable this functionality, the following are the steps adopted,
Step 1 : Adding the dependency
In pom.xml, add the below dependency
Step 2 : Enabling the configuration
Add a config file to enable the cache name to store and retrieve the values.
Step 3 : Caching the request body
Either in the controller or service class, cache the request body.
- The requestModel mentioned in the above screenshot is the request body passed to the endpoint
Step 4 : Get the request body from the cache
Now the request body is present in the cache, you can get it by adding the code snippets in your class
var requestObject = cacheManager.getCache(“requestBody”).get(“body”).get();