We have REST services exposed via Spring MVC. We use a HandlerExceptionResolver
to log exceptions. We currently log the following:
It would make debugging easier if we could also log the JSON post data as well. Any suggestions on how to get this?
Use an HTTP POST request to send single or multiple RPC requests to the REST API. You can use the POST request to do device configuration.
Yes, you can make it work at least using WCF, it's bit different in MVC and Web API where you add attributes to methods like [GET] [POST] etc..
To post JSON to a REST API endpoint, you must send an HTTP POST request to the REST API server and provide JSON data in the body of the POST message. You also need to specify the data type in the body of the POST message using the Content-Type: application/json request header.
Always POST , and preferably with SSL (as in: https://... ). Because the parameters in GET get stored all over the place for caching reasons. So, if you boss needs a reason: security.
GET requests should be used to retrieve data when designing REST APIs; POST requests should be used to create data when designing REST APIs. Creating something is a side effect — if not the point. The HTTP GET method isn't supposed to have side effects. It's considered read-only for retrieving data.
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("]\n");
return f;
}
you may have to comment out
f.setIncludePayload(true);
There is no easy way to log the payload of the request/response. You can use a java web filter to intercept all the requests and responses and read the JSON data from the stream. But there is one problem, when you will read data from the stream the actual data will be exhausted from stream.
Therefore, you have to implement the wrapper of actual request and response object. Only the copied version of request response will be logged. We have implemented similar solution like follows and it satisfied our requirement:
http://www.wetfeetblog.com/servlet-filer-to-log-request-and-response-details-and-payload/431
http://angelborroy.wordpress.com/2009/03/04/dump-request-and-response-using-javaxservletfilter/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With