I have rest controllers that returns Json response. I have added interceptor which will add some element to the response as shown below.
@Override
public void postHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final ModelAndView modelAndView) throws Exception {
final long end = System.currentTimeMillis();
final long start = (Long) request.getAttribute("_start_time");
// how do I append to response by the difference of end - start?
super.postHandle(request, response, handler, modelAndView);
}
If I simply add anything to the response I get the following exception:
There was an exception java.lang.IllegalStateException:getOutputStream() has already been called for this response
Adding the “end-start” time to response is not the right place. As you may actually end up breaking the JSON response you are sending back.
What you need to do is –
Option 1 – Add a response header. Something like this
This way you have the data and you dont have to modifythe response. You can see the header in Firebug.
Option 2 – Add the above information in the log file so you dont have to change the servlet response.