Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

We have REST services exposed via Spring MVC. We use a HandlerExceptionResolver to log exceptions. We currently log the following:

  • The exception and its stack trace
  • The URL
  • The request headers

It would make debugging easier if we could also log the JSON post data as well. Any suggestions on how to get this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
211 views
Welcome To Ask or Share your Answers For Others

1 Answer

Add this to the class representing the configuration for the application:

import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.filter.AbstractRequestLoggingFilter;

....

@Bean
public Filter loggingFilter(){
    AbstractRequestLoggingFilter f = new AbstractRequestLoggingFilter() {

        @Override
        protected void beforeRequest(HttpServletRequest request, String message) {
            System.out.println("beforeRequest: " +message);
        }

        @Override
        protected void afterRequest(HttpServletRequest request, String message) {
            System.out.println("afterRequest: " +message);
        }
    };
    f.setIncludeClientInfo(true);
    f.setIncludePayload(true);
    f.setIncludeQueryString(true);

    f.setBeforeMessagePrefix("BEFORE REQUEST  [");
    f.setAfterMessagePrefix("AFTER REQUEST    [");
    f.setAfterMessageSuffix("]
");
    return f;
}

you may have to comment out

   f.setIncludePayload(true);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...