swagger修改注解自动加载,支持jrebel

swagger不管是那个版本,修改了注解和模型属性描述都需要重启才能生效,而且重启是很耗时的,对于使用jrebel的用户来说更不友好。

只需简单几步就可以实现swagger注解的动态加载

  • 编写swaggerAutoReload类
import com.fasterxml.classmate.TypeResolver;
import java.util.List;
import org.springframework.stereotype.Component;
import springfox.documentation.PathProvider;
import springfox.documentation.spi.service.RequestHandlerProvider;
import springfox.documentation.spi.service.contexts.Defaults;
import springfox.documentation.spring.web.DocumentationCache;
import springfox.documentation.spring.web.plugins.AbstractDocumentationPluginsBootstrapper;
import springfox.documentation.spring.web.plugins.DocumentationPluginsManager;
import springfox.documentation.spring.web.scanners.ApiDocumentationScanner;

@Component
public class SwaggerAutoReload extends AbstractDocumentationPluginsBootstrapper {
    public SwaggerAutoReload(DocumentationPluginsManager documentationPluginsManager, List<RequestHandlerProvider> handlerProviders, DocumentationCache scanned, ApiDocumentationScanner resourceListing, Defaults defaults, TypeResolver typeResolver, PathProvider pathProvider) {
        super(documentationPluginsManager, handlerProviders, scanned, resourceListing, defaults, typeResolver, pathProvider);
    }

    public void reload() {
        super.bootstrapDocumentationPlugins();
    }
}
  • 编写swaggerFilter过滤器
public class SwaggerFilter implements Filter {

    private SwaggerAutoReload swaggerAutoReload;

    public SwaggerFilter(SwaggerAutoReload swaggerAutoReload) {
        this.swaggerAutoReload=swaggerAutoReload;
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }

    @SneakyThrows
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //在文档页面刷新的时候,执行自动加载
        swaggerAutoReload.reload();
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
  • 找一个Configuration类,写一个bean
@Configuration
public class Knife4jConfiguration {

    @Autowired
    private SwaggerAutoReload swaggerAutoReload;

    @Bean
    public Docket defaultApi2() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("Java接口文档")
                        .description("Java接口文档")
                        .version("1.0")
                        .build())
                //分组名称
                .groupName("后端接口")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .directModelSubstitute(Timestamp.class, Date.class);
        return docket;
    }

    @Bean
    public FilterRegistrationBean registFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new SwaggerFilter(swaggerAutoReload));
        registration.addUrlPatterns("/doc.html/*");
        registration.setName("SwaggerFilter");
        registration.setOrder(1);
        return registration;
    }

}
  • 然后重启项目,刷新文档页面就可以自动加载了

发表评论