溫馨提示×

溫馨提示×

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

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

使用SpringMVC怎么對Swagger進(jìn)行整合

發(fā)布時間:2020-12-01 16:51:07 來源:億速云 閱讀:276 作者:Leah 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)使用SpringMVC怎么對Swagger進(jìn)行整合,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

配置

1、引入相關(guān)jar包:

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

2、創(chuàng)建java配置類

@Configuration
@EnableSwagger2
public class Swagger2 {
  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
         // 文檔標(biāo)題
        .title("wish")
        // 文檔描述
        .description("https://github.com/handexing").termsOfServiceUrl("https://github.com/handexing")
        .version("v1")
        .build();
  }
  @Bean
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .select()
        // 指定controller存放的目錄路徑
        .apis(RequestHandlerSelectors.basePackage("com.wish.controller"))
        .paths(PathSelectors.any())
        .build();
  }
}

3、編寫接口文檔測試

@RequestMapping(value = "testSawgger", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
@ApiOperation(value = "測試swagger", httpMethod = "POST", notes = "testSawgger")
public ExecuteResult<Boolean> addUser(@ApiParam(value = "參數(shù)", required = true) Long id) {
  ExecuteResult<Boolean> result = new ExecuteResult<Boolean>();
  try {
    result.setSuccess(true);
  } catch (Exception e) {
    result.setSuccess(false);
  }
  return result;
}

說明:

@ApiOperation:用在方法之上

1、value: 表示接口名稱

2、notes: 表示接口詳細(xì)描述

3、httpMethod:表示接口請求方法類型

@ApiParam:用在方法參數(shù)上

1、required:表示參數(shù)是否必須傳

2、name:表示參數(shù)名稱

3、value:表示參數(shù)描述

關(guān)于使用SpringMVC怎么對Swagger進(jìn)行整合就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI