Swagger 2.9.2版本不支持Webflux

然后查阅资料Webflux需要使用Swagger3.0
我找了好久资料
好多配置都没有成功

最后一不小心成功了,发现其实并不需要多少配置
我们直接上配置
gradle

compile group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'

maven

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

直接导入就能使用了
然后应用主类增加注解@EnableOpenApi,删除之前版本的SwaggerConfig.java,启动项目,访问地址:http://localhost:8200/swagger-ui/index.html,注意2.x版本中访问的地址的为http://localhost:8200/swagger-ui.html

2.配置资源目录
注意:这个类和swagger没有关系,是配置跨域的

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    //这个和swagger没有关系,是配置跨域的
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST","DELETE","OPTIONS")
                .allowCredentials(false).maxAge(3600);
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.
                addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
                .resourceChain(false);
        super.addResourceHandlers(registry);
    }

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

默认是这样的,不影响你使用
在这里插入图片描述
默认是以上界面,当然之前的配置还可以继续配置,只不过是可选配置,那我们继续加上

@Configuration
public class SwaggerConfiguration {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.aiprose.mbp.controller"))
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboot mybatis plus框架demo")
                .description("springboot mybatis plus框架demo")
                .contact(new Contact("nelson", "https://www.aiprose.com/", "mail_yanpeng@163.com"))
                .version("1.0")
                .build();
    }
}

在这里插入图片描述

原文地址

https://www.aiprose.com/blog/127

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐