I want to create a middleware when request comes on controller like we did in express Nodejs.
var myLogger = function (req, res, next) {
console.log('LOGGED')
next()
}
app.use(myLogger)
api.post('/', function(req,res) {
// enter code here...
})
Is it possible in spring boot doing the same functionality Like
@RestController
@RequestMapping("/api")
private class TestApiMiddleware {
// middleware...
@RequestMapping(value = "/save", method = RequestMethod.POST)
protected list() throws Exception {
// code...
}
}
The easiest way would be to use a Filter as follows:
@Configuration
public class FilterConfiguration {
@Bean
public Filter loggerFilter() {
return new Filter() {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
logger.info("LOGGED");
chain.doFilter(request, response);
}
};
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With