接口文檔之 springboot 集成 swagger 和 swagger-bootstrap-ui,感覺還不錯


總結:其實我是討厭寫文檔的,但是又不得不寫文檔,寫接口文檔的工具 有很多,如: CrapApi ,APIJSON,superapi,java-api-doc ,sosoapi,yapi

        這里隨便寫下一下 swagger 

        常用注解 如下:

 @Api:用在類上,說明該類的作用。

@ApiOperation:注解來給API增加方法說明。

@ApiImplicitParams : 用在方法上包含一組參數說明。

@ApiImplicitParam:用來注解來給方法入參增加說明。

@ApiResponses:用於表示一組響應

@ApiResponse:用在@ApiResponses中,一般用於表達一個錯誤的響應信息

* code:數字,例如400

* message:信息,例如"請求參數沒填好"

* response:拋出異常的類

@ApiModel:描述一個Model的信息(一般用在請求參數無法使用@ApiImplicitParam注解進行描述的時候)

* @ApiModelProperty:描述一個model的屬性

注意:@ApiImplicitParam的參數說明:

paramType:指定參數放在哪個地方
header:請求參數放置於Request Header,使用@RequestHeader獲取

query:請求參數放置於請求地址,使用@RequestParam獲取

path:(用於restful接口)-->請求參數的獲取:@PathVariable

body:(不常用)

form(不常用)

name:參數名
dataType:參數類型
required:參數是否必須傳 true | false
value:說明參數的意思
defaultValue:參數的默認值

整合 步驟:
1.引入jar
<!-- swagger用於定義API文檔 -->
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.spring4all/swagger-spring-boot-starter -->
<!--<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.8.0.RELEASE</version>
</dependency>-->

<!--美化swagger-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.3</version>
</dependency>
2.配置
@Configuration
@EnableSwagger2
public class Swagger2 {

@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.web"))
//.paths(AppUtility.isProd() ? PathSelectors.none() : PathSelectors.any())
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("測試swagger")
.description("展示swagger界面")
.termsOfServiceUrl("http://localhost:8089/swagger-ui.html")
.contact(new Contact("lyc88", "http://localhost:8089/swagger-ui.html", "984006207@qq.com"))
.version("1.0")
.build();
}
}

排除靜態文件
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");

registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");

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

3.就可以快樂的看文檔了
http://127.0.0.1:8089/doc.html

隨便寫了幾個例子如下:

Spring Boot 2.X(十五):集成 Swagger2 開發 API 文檔(在線+離線)

https://www.cnblogs.com/zwqh/p/11803318.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM