简体   繁体   中英

How to get request/response payload using Servlet Filter

I am implementing Servlet Filter around Rest and SOAP services to be able to capture the request and response. The filter is invoked before the call goes to rest service but I am unable to get the elements of request from the payload. So the filter initializes then the call goes to doFilter and then to rest service but on the request and response variables I don't see anything related to the rest request and response. Hope the question is clear.

Filter Class

public class ESignLoggingInterceptor implements  Filter {

private static final Logger logger = Logger.getLogger( ESignLoggingInterceptor.class.getSimpleName() );

@Override
public void init( FilterConfig config ) throws ServletException {
    System.out.println( "Logging filter initiated" );

}

@Override
public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException {
    String path = null;
    if( request != null && request instanceof HttpServletRequest ) {
         path = ( ( HttpServletRequest ) request).getRequestURL().toString();
    }
    chain.doFilter( request, response );
    System.out.println("Request URL:" + " " + path );
}

@Override
public void destroy() {
}

}

web.xml

<servlet>
    <servlet-name>Jersey Spring</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.mercuryinsurance.esignature.client.rest.service</param-value>
    </init-param>
    <init-param>        
        <param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>        
        <param-value>/WEB-INF/jsps/rest</param-value>    
    </init-param>    
    <init-param>
        <param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>        
        <param-value>/(resources|(WEB-INF/jsp))/.*</param-value>    
    </init-param> 
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Spring</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>LoggingFilter</filter-name>
    <filter-class>com.mercuryinsurance.esignature.common.logging.ESignLoggingInterceptor</filter-class>
</filter>

<filter-mapping>
    <filter-name>LoggingFilter</filter-name>
     <url-pattern>/rest/*</url-pattern>
</filter-mapping>

While the JAX-RS stack is a layer on top of servlets, you're better off to use the standards. There are a few ways to do this. If you Google for something like "Jersey JAX-RS logging" you can find Jersey specific ways to log - ie like this link . Otherwise, if you want to stick with JEE standards, you'll need a ContainerRequestFilter for the inputs and a WriterInterceptor for the outputs. But a disclaimer - I have no idea how this interacts with Spring since that's not JEE.

I learned a great amount of information from this stackoverflow answer too but, again, that is the standards way.

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