温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot中如何集成swagger

发布时间:2021-06-11 16:29:06 来源:亿速云 阅读:176 作者:Leah 栏目:编程语言

本篇文章给大家分享的是有关springboot中如何集成swagger,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

springboot集成swagger

1、pom.xml中引入:

<dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.9.2</version>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.9.2</version>
    </dependency>

2、配置类:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
  @Bean
  public Docket swaggerSpringMvcPlugin() {

    return new Docket(DocumentationType.SWAGGER_2)
        .select()
        //加了ApiOperation注解的类,才生成接口文档
        .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
        .build();
  }

}

3、controller相应的注解:@ApiOperation

@ApiOperation(value = "用户登录",notes = "")
  @PostMapping("/loginOn")
  public ResponseMessage loginOn(@RequestBody @Valid UserReq userReq){
    ResponseMessage responseMessage = userServiceImp.loginOn(userReq);
    return responseMessage;
  }

最后本地默认访问:http://localhost:8080/swagger-ui.html

既可以看到相关接口效果图:

springboot中如何集成swagger

访问页失败的可能原因:

1》》访问方法本来就是404错误:在sprigboot中有个重要的概念叫做:约定优于配置:

springboot启动的时候如果没有指定扫描的包路径时,默认会去加载其当前包及子包下的组件,这里需要注意

springboot中如何集成swagger

如果把启动类放入service包下,页面就会访问不到:

springboot中如何集成swagger

springboot中如何集成swagger

2》》SwaggerConfig 类的写法有问题:Docket方法挺多的,这里需要注意:

springboot中如何集成swagger

@Configuration
@EnableSwagger2
public class SwaggerConfig {
  @Bean
  public Docket swaggerSpringMvcPlugin() {

    return new Docket(DocumentationType.SWAGGER_2)
        .select()
        //加了ApiOperation注解的类,才生成接口文档
        .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
        .build();
  }

}

3》》配置拦截器时是否进行了拦截:

在实现WebMvcConfigurer接口时,我们再配置拦截器时,需要对相应的请求进行过滤放行,比如静态资源,登录请求等

@Configuration
public class WebConfig implements WebMvcConfigurer {
  /**
   * 配置拦截器
   * @param registry
   */
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new RequestInterceptor()).addPathPatterns("/**").excludePathPatterns("/user/login")
        //排除swagger
    .excludePathPatterns("/swagger-resources/**", "/webjars/**",
        "/v2/**", "/swagger-ui.html/**");
  }

}

有的代码是通过重写WebMvcConfigurer的addResourceHandlers方法:

/**
   * 添加静态资源--过滤swagger-api (开源的在线API文档)
   * @param registry
   *//*
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //过滤swagger
    registry.addResourceHandler("swagger-ui.html")
        .addResourceLocations("classpath:/META-INF/resources/");

    registry.addResourceHandler("/webjars/**")
        .addResourceLocations("classpath:/META-INF/resources/webjars/");

    registry.addResourceHandler("/swagger-resources/**")
        .addResourceLocations("classpath:/META-INF/resources/swagger-resources/");

    registry.addResourceHandler("/swagger/**")
        .addResourceLocations("classpath:/META-INF/resources/swagger*");

    registry.addResourceHandler("/v2/api-docs/**")
        .addResourceLocations("classpath:/META-INF/resources/v2/api-docs/");

  }*

以上就是springboot中如何集成swagger,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI