Hot reloading of Spring Swagger API-docs during development (hints + question)
See original GitHub issueA hint for anybody wanting to use hot reload capabilities during API development to refresh the API-docs:
To enable hot reload of the apidocs during development I did: 1.) Activate Spring-Loaded 1.2.0 or higher java -javaagent:<pathTo>/springloaded-{VERSION}.jar -noverify (https://github.com/spring-projects/spring-loaded) This makes it possible to add new methods/fields/etc. during development and access it immediately using the Request Path. However, the mappings updated in AbstractHandlerMethodMapping.registerHandlerMethod do not get propagated to SpringSwaggerConfig.handlerMappings, so they won’t show up in the /api-docs
2.) To allow API-changes in existing controller classes to show up, I did the following:
a) add a Controller class to allow refresh:
....
@RestController
@ApiIgnore
public class SpringSwaggerController {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private SpringSwaggerConfig springSwaggerConfig;
@RequestMapping(method = RequestMethod.GET, value = "/refresh")
@ResponseBody
public String refresh() {
SwaggerReloadUtil.reload(applicationContext, springSwaggerConfig);
return "Swagger config refreshed";
}
}
SwaggerReloadUtil (has to be in the same package com.mangofactory.swagger.plugin because initialize() is protected:
package com.mangofactory.swagger.plugin;
...
public class SwaggerReloadUtil {
public static Map<String, SwaggerSpringMvcPlugin> reload(
ApplicationContext applicationContext,
SpringSwaggerConfig springSwaggerConfig) {
new SwaggerSpringMvcPlugin(springSwaggerConfig).build().initialize();
Map<String, SwaggerSpringMvcPlugin> plugins = BeanFactoryUtils
.beansOfTypeIncludingAncestors(applicationContext,
SwaggerSpringMvcPlugin.class);
return plugins;
}
}
Then by calling http://localhost:8080/refresh the Swagger configuration get’s rebuilt and API changes will show up.
This is however, valid only for all changes at the operation level (@ApiOperation, @ApiResponses). Changes in a Controller class (@Api) or adding new Controllers won’t show up.
3.) To allow new controller classes to be picked up by Spring, you can use JHipster Core: http://tux2323.blogspot.co.at/2014/06/hot-reload-support-with-spring-loaded.html
This allows you to use new Controllers immediately and test them using the Tomcat container.
However, here the new controllers don’t get picked up by ApiListingReferenceScanner because the SpringSwaggerConfig.handlerMappings are not refreshed. So these new controllers currently don’t show up in the Swagger API-docs listing. Only a complete restart seems to re-read the SpringSwaggerConfig.handlerMappings.
Question: can you at least make the SpringSwaggerConfig.handlerMappings accessible, so they can be re-injected from outside?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:2
- Comments:9 (7 by maintainers)
I am sorry to necro this issue, but is there a way to say refresh the swagger configuration using springfox upon changing restcontroller without the need of restarting the whole application?
So…reloading still not working in version 2.5.0?