user3489820
user3489820

Reputation: 1519

Spring controller - logging request and response body

I have a spring application which exchanges JSON with the mobile. Spring controller looks like this:

@RequestMapping(value = "/register", method = RequestMethod.POST, headers = {"Content-type=application/json"})
public String register(@RequestBody @Valid UserRegistrationRequest urf, BindingResult bindingResult) {
    return toJson(someResponse);
}

I wonder, what is the best way to log http request body and response body? At the moment, I have custom json message converter and it logs a request body, before creating a bean out of json. and I use CustomTraceInterceptor to log a response body. Unfortunately, CustomTraceInterceptor doesn't allow to log request body.

Any advice for better solutions would be highly appreciated!

Thank you in advance.

Upvotes: 2

Views: 7685

Answers (1)

NimChimpsky
NimChimpsky

Reputation: 47290

Extend HandlerInterceptorAdapter, and override postHandle. Which has request and response injected into it.

You can also use new HttpServletResponseWrapper((HttpServletResponse) response) which has a more friendly api, and spring probably has even nicer wrapper as well ...

Upvotes: 3

Related Questions