I want to log all the incoming requests which will be in json format.I am using spring @RestController and @RequestBody annotations to bind the incoming json content to java objects.But i want to log these requests to logger files.I have searched around for objectmapper and jacksonbinding.
@RestController
public class restClassName{
@RequestMapping(value={"/uri"})
public ObjectResponse functionRestName(@RequestBody ObjectRequest or){
String jsonInString = mapper.writeValueAsString(staff);//Redundant stuff as the request json is already read by MappingJackson2HttpMessageConverter
logger.info("request::"+jsonInString)
return instance;
}
}
But this seems to be a rendundant way of doing.Since MappingJackson2HttpMessageConverter already reads the httprequest to convert json request to java object.I just need to log the json before MappingJackson2HttpMessageConverter converts the request json to java object.
The simplest way to achieve it is to use
CommonsRequestLoggingFilter
as described in below pseudo code.Then in application.properties file add the follwing line.
This will log all the requests, please follow the link to CommonsRequestLoggingFilter api doc for more customization.