java-从球衣迁移到spring-mvc / rest:ContainerRequestFilter,ContainerResponseFilter

由于业务需求,我需要从球衣迁移到spring-mvc / rest …

我在spring-mvc中搜索与球衣的ContainerRequestFilter,ContainerResponseFilter等价的东西.

有任何想法吗?

解决方法:

我认为您正在寻找的是HandlerInterceptor.您不会在两类请求/响应风格中找到它,但是两个过滤器方法都以preHandle和postHandle的形式包含在同一类中,分别相当于请求过滤器和响应过滤器.拦截器包装处理程序调用.这是一个例子:

拦截器:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LogginHandlerInterceptor implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest hsr, 
                             HttpServletResponse hsr1, Object handler) throws Exception {
        System.out.println("preHandle - " + handler);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest hsr, HttpServletResponse hsr1, 
                           Object handler, ModelAndView mav) throws Exception {
        System.out.println("postHandle - " + handler);
    }

    @Override
    public void afterCompletion(HttpServletRequest hsr, HttpServletResponse hsr1, 
                                Object handler, Exception excptn) throws Exception {
        System.out.println("afterCompletion - " + handler);
    }
}

配置:

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"..."})
public class WebAppConfiguration extends WebMvcConfigurerAdapter {
    ...
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LogginHandlerInterceptor());
    }
    ...
}

也可以看看:

> Intercepting Requests with HandlerInterceptor
> MVC Config Interceptors

上一篇:java-无法使用Jersey解析RESTful应用程序中的日期


下一篇:java-如何从多部分表单数据请求中删除http构件?