Openapi-generator: [Java][Spring] How to inject HttpServletResponse in controller

Created on 15 Jan 2019  路  5Comments  路  Source: OpenAPITools/openapi-generator

Is is possible to inject a HttpServletResponse in a generated controller method?

Bug

Most helpful comment

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.

All 5 comments

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 }

Was this page helpful?
0 / 5 - 0 ratings