question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hot reloading of Spring Swagger API-docs during development (hints + question)

See original GitHub issue

A 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:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
divStarcommented, Jul 14, 2019

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?

0reactions
qfrankcommented, Nov 23, 2016

So…reloading still not working in version 2.5.0?

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Swagger 2 Issue - Spring Boot - Stack Overflow
I wonder now if there is a way to fix this. SwaggerConfig: package com.animes.apirest.config; import springfox.documentation.swagger2.
Read more >
Spring Boot Reference Documentation
Try the How-to documents. They provide solutions to the most common questions. Learn the Spring basics. Spring Boot builds on many other Spring...
Read more >
Setting up a dev environment - Swagger Documentation
Swagger UI includes a development server that provides hot module reloading and unminified stack traces, for easier development.
Read more >
11 Spring Boot Interview Questions That Make You Think
If working with a web application, you will start with spring-boot-starter-web and then add the appropriate starter from the documentation, once ...
Read more >
Docs • Svelte
This page contains detailed API reference documentation. ... In development mode (see the compiler options), a warning will be printed if no default...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found