简体   繁体   English

如何使用 Servlet 过滤器获取请求/响应负载

[英]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.我正在围绕 Rest 和 SOAP 服务实现Servlet Filter ,以便能够捕获请求和响应。 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.所以过滤器初始化然后调用转到 doFilter 然后到休息服务但是在请求和响应变量上我没有看到任何与休息请求和响应相关的东西。 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网页.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.虽然 JAX-RS 堆栈是位于 servlet 之上的一层,但您最好使用这些标准。 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 .如果您在 Google 上搜索诸如“Jersey JAX-RS 日志记录”之类的内容,您可以找到 Jersey 特定的日志记录方式—— 例如这个链接 Otherwise, if you want to stick with JEE standards, you'll need a ContainerRequestFilter for the inputs and a WriterInterceptor for the outputs.否则,如果您想坚持 JEE 标准,您将需要一个用于输入的 ContainerRequestFilter 和一个用于输出的 WriterInterceptor。 But a disclaimer - I have no idea how this interacts with Spring since that's not JEE.但是免责声明 - 我不知道它是如何与 Spring 交互的,因为那不是 JEE。

I learned a great amount of information from this stackoverflow answer too but, again, that is the standards way.我也从这个 stackoverflow 答案中学到了大量信息,但同样,这是标准方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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