t77*_*777 4 java rest spring spring-mvc jersey
由于业务需求,我需要从球衣迁移到spring-mvc / rest ...
我正在寻找在弹簧MVC的东西,相当于新泽西州的ContainerRequestFilter,ContainerResponseFilter。
有任何想法吗?
I think what you're looking for is the HandlerInterceptor. You won't find it in the two-class request/response flavor, but both filter methods are included in the same class, in the form of preHandle and postHandle, which is the equivalent of request-filter and response-filter, respectively. The interceptor wraps the handler call. Here's an example:
Interceptor:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
Config:
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());
}
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3441 次 |
| 最近记录: |