溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

springboot中如何集成swagger

發布時間:2021-06-11 16:29:06 來源:億速云 閱讀:207 作者: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

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女