开发者

Getting the body and response code from an HTTPServletResponse

开发者 https://www.devze.com 2023-04-10 03:23 出处:网络
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 writ

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 construc开发者_C百科tor 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消