Is is possible to inject a HttpServletResponse in a generated controller method?
as a workaround: you can inject HttpServletResponse and HttpServletRequest into the constructor of your controller. Spring will take care of scoping and make the correct objects available for you at execution time.
another workaround involving the use of import mappings at #4680
(late but hth to others looking on how to do this)
try it:
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
You can just autowire HttpServletResponse or HttpServletRequest into your controller
@Controller
public class YourController{
@Autowired
private HttpServletResponse httpServletResponse; // <- thread safe
@Autowired
private HttpServletRequest httpServletRequest; // <- thread safe
}
Most helpful comment
as a workaround: you can inject
HttpServletResponseandHttpServletRequestinto the constructor of your controller. Spring will take care of scoping and make the correct objects available for you at execution time.