0
我有以下Spring Boot
控制器和JavaEE
过滤器类在我的Spring Boot
基于REST
应用程序。请注意,我没有在这里配置web.xml
。如何在Spring Boot应用程序中注册JavaEE筛选器?
http://localhost:8080/api/products -> Returns 200
问题是拦截器/过滤器永远不会被调用。
ProductController.java
@RestController
@RequestMapping("/api")
public class ProductController {
@Inject
private ProductService productService;
//URI: http://localhost:8080/api/products
@RequestMapping(value = "/products", method = RequestMethod.GET)
public ResponseEntity<Iterable<Product>> getAllProducts() {
Iterable<Product> products = productService.getAllProducts();
return new ResponseEntity<>(products, HttpStatus.OK);
}
//URI: http://localhost:8080/api/products/50
@RequestMapping(value = "/products/{productId}", method = RequestMethod.GET)
public ResponseEntity<?> getProduct(@PathVariable Long productId) {
Product product = productService.getProduct(productId);
return new ResponseEntity<>(product, HttpStatus.OK);
}
}
SecurityFilter.java
@WebFilter(urlPatterns = {"/api/products/*"})
public class SecurityFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
//in the init method
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//THIS METHOD NEVER GETS CALLED AND HENCE THIS NEVER GETS PRINTED ON CONSOLE.
//WHY ?????
System.out.println("**********Into the doFilter() method...........");
final HttpServletRequest httpRequest = (HttpServletRequest) request;
final HttpServletResponse httpResponse = (HttpServletResponse) response;
//move ahead
chain.doFilter(httpRequest, httpResponse);
}
@Override
public void destroy() {
//nothing to implement
}
}
BasicRestApplication.java
@SpringBootApplication
public class BasicRestApplication {
public static void main(String[] args) {
SpringApplication.run(BasicRestApplication.class, args);
}
}
我已经通过这个链接How to add a filter class in Spring Boot?,但它没有给出清楚的想法,需要添加哪些地方注册过滤器Spring Boot
。