這篇文章將為大家詳細講解有關使用SpringMVC怎么對Swagger進行整合,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
配置
1、引入相關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、創建java配置類
@Configuration
@EnableSwagger2
public class Swagger2 {
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 文檔標題
.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 = "參數", 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: 表示接口詳細描述
3、httpMethod:表示接口請求方法類型
@ApiParam:用在方法參數上
1、required:表示參數是否必須傳
2、name:表示參數名稱
3、value:表示參數描述
關于使用SpringMVC怎么對Swagger進行整合就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。