升级使用SpringFox的swagger-ui 3.0.0版本

说明

你的项目已经使用了sawgger2.x版本。

升级

新特性

https://github.com/springfox/springfox

NOTE: Would love feedback to make this better

  • Remove explicit dependencies on springfox-swagger2

  • Remove any @EnableSwagger2… annotations

  • Add the springfox-boot-starter dependency

  • Springfox 3.x removes dependencies on guava and other 3rd party libraries (not zero dep yet! depends on spring plugin and open api libraries for annotations and models) so if you used guava predicates/functions those will need to transition to java 8 function interfaces.

  • 移除了2.x版本的冲突版本,移除了guava等

  • 移除了@EnableSwagger2

  • 新增了springfox-boot-starter

新增了starter

删除之前的依赖,直接使用最新的starter方式。

    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.1.RELEASE
    



    io.springfox
    springfox-boot-starter
    3.0.0

然后应用主类增加注解@EnableOpenApi,删除之前版本的SwaggerConfig.java,启动项目,访问地址:http://localhost:8088/swagger-ui/index.html,注意2.x版本中访问的地址的为http://localhost:8088/swagger-ui.html
升级使用SpringFox的swagger-ui 3.0.0版本_第1张图片
变化还是很大的。

拦截配置

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.
                addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
                .resourceChain(false);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/swagger-ui/")
                .setViewName("forward:/swagger-ui/index.html");
    }
}

可选配置

@Configuration
public class Swagger3Config {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Swagger3接口文档")
                .description("文档描述")
                .contact(new Contact("fanl", "#", "[email protected]"))
                .version("1.0")
                .build();
    }
}

SpringSecurity 拦截

    private static final String[] AUTH_WHITELIST = {
            // -- swagger ui
            "/swagger-ui.html",
            "/swagger-ui/*",
            "/swagger-resources/**",
            "/v2/api-docs",
            "/v3/api-docs",
            "/webjars/**"
    };
//追加
 .antMatchers(AUTH_WHITELIST).permitAll()

你可能感兴趣的:(Java开发)