How to register HttpRequest and HttpResponse in a file?

Can anyone explain some methods of registering HttpRequest and HttpResponse in a file.

We are using Spring MVC / Spring Rest.

We want to intercept the request before processing it and register it. The same way to intercept the response before sending it and register it.

Thank you very much in advance.

+4
spring spring-data spring-data-rest spring-mvc
source share
2 answers

For logging, the Spring request has an AbstractRequestLoggingFilter class (well, in fact, one of the subclasses). This can be used to register an incoming request (before and after processing).

Depending on the configuration, this may include the payload, client information, and full URL (including erquest parameters). All three of these are disabled by default, but can be enabled through configuration (see Javadoc for more information).

 <filter> <filter-name>requestLoggingFilter</filter-name> <filter-class>org.springframework.web.filter.CommonsRequestLoggingFilter</filter-class> <init-param> <param-name>includeClientInfo</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>includePayload</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>includeQueryString</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>requestLoggingFilter</filter-name> <servlet-name>dispatcherServlet</servlet-name> </filter-mapping> 

Now the filter will log everything, using the Commons logging log, into the log file.

+7
source

The accepted answer is already correct by adding a setting based on annotation. Add a bean to the configuration.

 @Bean public CommonsRequestLoggingFilter requestLoggingFilter() { CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter(); loggingFilter.setIncludeClientInfo(true); loggingFilter.setIncludeQueryString(true); loggingFilter.setIncludePayload(true); return loggingFilter; } 
+5
source

All Articles