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

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

java - How can I log RESTful post data?

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

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

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...