I have the following Spring Boot
controller and a JavaEE
filter classes in my Spring Boot
based REST
application. Please note that I do not have a web.xml
configured here.
http://localhost:8080/api/products -> Returns 200
The problem is that the interceptor/filter never gets called.
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);
}
}
I have gone through this link How to add a filter class in Spring Boot? but it does not give a clear idea on what needs to be added where to register the filter with Spring Boot
.