欢迎来到入门教程网!

Java

当前位置:主页 > 软件编程 > Java >

springboot集成swagger过程解析

来源:本站原创|时间:2020-01-10|栏目:Java|点击:

这篇文章主要介绍了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

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

访问页失败的可能原因:

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

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

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

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

@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/");

  }*

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

上一篇:Spring实战之使用Resource作为属性操作示例

栏    目:Java

下一篇:Spring事务管理原理及方法详解

本文标题:springboot集成swagger过程解析

本文地址:https://www.xiuzhanwang.com/a1/Java/8758.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有