Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access HttpServletResponse in Spring Component?

I have a spring-boot app with swagger code generator. Since the controllers are autogenerated from swagger yaml I don't think I can modify the controllers to access the HttpServletResponse.

Is there a way to access the HttpServletResponse from a class annotated with @Component?

I am trying to access the HttpServletResponse instance in order to set a cookie.

like image 937
Nelson Avatar asked Sep 07 '25 13:09

Nelson


1 Answers

If you don't want to touch the controller at all would be to make your bean a spring filter bean. This article here gives you details how to declare such bean. Once declare you need to bind it to url pointing to your controller method.

https://www.baeldung.com/spring-boot-add-filter

It depends on What your final goal is.

like image 116
Alexander Petrov Avatar answered Sep 09 '25 17:09

Alexander Petrov