1.如果你的controller没反应,访问网页也没有404,告诉你一般是filter搞的鬼,因为你拦截了所有请求
2.开始自定义Filter
import org.java_websocket.WebSocketImpl;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import java.io.IOException;
public class StartFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
一般都是实现Filter类,重写方法
3.注册拦截(spring boot版本)
package com.example.demo.websocket;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FilterConfiguration {
@Bean
public FilterRegistrationBean filterDemo4Registration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
//注入过滤器
registration.setFilter(new StartFilter());
//拦截规则
registration.addUrlPatterns("/oo/*");
//过滤器名称
registration.setName("WebSocketFilter");
//过滤器顺序
registration.setOrder(1);
return registration;
}
}
注意里面标红的地方,如果写成"/*",或者没有写,你等着controller全部dead吧
其次还有一个写法
在拦截器class 上添加注解@WebFilter(urlPatterns={"/app/online"})
然后在启动类(**Application.java )上添加注解@ServletComponentScan