前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringFox Swagger 3.0.0 适配SpringMvc、SpringWebflux

SpringFox Swagger 3.0.0 适配SpringMvc、SpringWebflux

作者头像
码农笔录
发布2020-10-30 11:06:11
1.1K0
发布2020-10-30 11:06:11
举报
文章被收录于专栏:码农笔录码农笔录

原文地址 https://www.aiprose.com/blog/127

现在后台基本都用到了swagger,无论开发人员调试测试,还是前端用,都很方便,前段时间如果用SpringWebflux的话,只能用3.0快照版,不过最近已经发布了正式版,时隔2年多,新版本还是有很大的变化。

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

1.添加依赖

是的你没看错,直接提供了springboot的start

gradle

代码语言:javascript
复制
compile group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'

maven

代码语言:javascript
复制
<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.配置资源目录

代码语言:javascript
复制
@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");
    }
}

默认是这样的,不影响你使用

默认是以上界面,当然之前的配置还可以继续配置,只不过是可选配置,那我们继续加上

代码语言:javascript
复制
@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();
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-10-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档