1.在xml 中添加Swagger 相關(guān)依賴
<!-- springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2.配置Swagger
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
/*
* 1.配置生成的文檔信息
* 2.配置生成規(guī)則
* */
//Docket封裝接口文檔信息
@Bean
public Docket
getDocket(){
//創(chuàng)建封面信息對象
ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
apiInfoBuilder.title("《xx項目》接口說明")
.description("此文檔詳細(xì)說明了xxx項目接口規(guī)范")
.version("v 0.0.1")
.contact(new Contact("","",""));
ApiInfo apiInfo = apiInfoBuilder.build();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo)
.select()
.apis(RequestHandlerSelectors.basePackage("com.xxxxxx.controller"))
.build();
return docket;
}
}
3.啟動項目,訪問 Swagger UI
訪問地址:http://localhost:xxx/swagger-ui.html出現(xiàn)下面界面則配置成功
4.更改界面風(fēng)格
4.1 添加依賴
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
4.2 啟動項目,并訪問
訪問地址:http://localhost:xxxx/doc.html
4.3效果
5.Swagger 注解
常用的注解
@API類注解,在控制器類添加此注解,可以對控制器類進(jìn)行功能說明
示例:@Api(value = "提供xx相關(guān)接口",tags = "xx管理")
@ApiOperation是用于構(gòu)建Api文檔的注解,它用于標(biāo)識一個HTTP請求的方法,并描述該方法的作用、請求方式、響應(yīng)等信息。
@ApiOperation(value = "XXXX接口", httpMethod = "POST", notes = "XXXXXX")
@ApiModelProperty:用于定義API模型的字段,包括字段名、描述、類型等信息。
@ApiModelProperty(value = "xxxx",required = true,example = "xxxx")//required = true 表示該字段為必填字段,example 表示:示例xxx
@ApiIgnore 注解的主要作用是忽略特定的類、方法或方法參數(shù),使其在 Swagger 生成的 API 文檔中不顯示。
用法:
@ApiIgnore 注解可以應(yīng)用于以下三個方面:
類:當(dāng)應(yīng)用于類時,整個類將被忽略,不會在 Swagger 生成的 API 文檔中顯示。
方法:當(dāng)應(yīng)用于方法時,該方法將被忽略,不會在 Swagger 生成的 API 文檔中顯示。
參數(shù):當(dāng)應(yīng)用于方法參數(shù)時,該參數(shù)將被忽略,不會在 Swagger 生成的 API 文檔的參數(shù)列表中顯示。
@ApiParam:用于描述單個入?yún)?。可以指定參?shù)的名稱、類型、是否必填以及描述信息。
用法:
@ApiParam(name = "id", type = "integer", required = true, value = "The ID of the resource")
@RequestBody:用于描述整個請求體??梢灾付ㄕ埱篌w的類型以及是否必填。
用法:
@RequestBody @ApiParam(name = "requestBody", type = "object", required = true, value = "The request body")
@RequestParam:用于描述URL中的查詢參數(shù)??梢灾付▍?shù)的名稱、類型、是否必填以及描述信息。
用法:
@RequestParam(name = "id", type = "integer", required = true, value = "The ID of the resource")
@PathVariable:用于描述URL中的路徑參數(shù)??梢灾付▍?shù)的名稱、類型、是否必填以及描述信息。
用法:
@PathVariable(name = "id", type = "integer", required = true, value = "The ID of the resource")
@RequestHeader:用于描述請求頭中的參數(shù)??梢灾付▍?shù)的名稱、類型、是否必填以及描述信息。
用法:
@RequestHeader(name = "Authorization", type = "string", required = true, value = "The authorization token")
6.遇見的問題文章來源:http://www.zghlxwxcb.cn/news/detail-700183.html
@ApiModelProperty(value = "xxxx",example = "xxxx")
變量上添加了注解 但是不起作用原因:
1.屬性名或?qū)傩灾挡环弦?guī)范。例如,屬性名應(yīng)該遵循駝峰命名法,且第二個字母不能大寫。另外,對于Boolean類型的字段,字段名應(yīng)該以is開頭。(我遇見的是變量命名不規(guī)范導(dǎo)致的)
2.在Controller接口層,需要直接返回使用@ApiModel注解的實體類,或者集合-泛型為使用@ApiModel注解的實體類。
文章來源地址http://www.zghlxwxcb.cn/news/detail-700183.html
到了這里,關(guān)于java 整合 swagger-ui 步驟的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!