2
votes

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.

1
Write a JEE filter (or a spring variant of a filter) and perform logging in that. - DwB

1 Answers

3
votes

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 ...