简体   繁体   中英

Getting the body and response code from an HTTPServletResponse

I'm attempting build a simple logging system for when our client accesses out API. We're using Spring to wire up controllers and handlers. I'm looking at Spring's Interceptor functionality to write a postHandle() method. Unfortunately, unlike all the code samples I've seen here, HttpServletResponse does not have, for example, a getStatus() method. I'm supposing that we're using the wrong version of Java or something.

We need the body and response code from the HttpServletResponse object: how can I get those?

EDIT: We went with a filter:

public void doFilter(
    ServletRequest request,
    ServletResponse response,
    FilterChain chain) throws IOException, ServletException {

    RichHttpServletResponse richResponse=new RichHttpServletResponse((HttpServletResponse)response);
    chain.doFilter(request, richResponse);
}

RichHttpServletResponse takes a servlet response as an argument in its constructor and overrides some of the methods, such as sendError() and passes the values through to the actual servlet response. The webmvc-config XML looks something like this:

<bean id="filterChainProxy" class="org.springframework.security.web.FilterChainProxy">
  <sec:filter-chain-map path-type="ant">
    <sec:filter-chain pattern="/**"
      filters="requestObjectFilter" />
  </sec:filter-chain-map>
</bean>

With a bean def below.

You might be able to call getOutputStream() and read that, but I'd be concerned about it screwing up the stream. There are some reset methods, but I'm not familiar with what they do myself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM