前言
在集成springfox-swagger2之前,我也嘗試著集成了swagger-springmvc,方式差不多,但是swagger-springmvc相對麻煩一點,因為要把它的靜態(tài)文件copy到自己的項目中。所以還是用新版本的。
至于兩者有什么不同,為什么進行版本變更請參見官方說明文檔
方法如下
這里先寫下需要的pom.xml配置(我引用的2.4.0,相對穩(wěn)定)
1
2
3
4
5
6
7
8
9
10
|
< dependency > < groupId >io.springfox</ groupId > < artifactId >springfox-swagger2</ artifactId > < version >2.4.0</ version > </ dependency > < dependency > < groupId >io.springfox</ groupId > < artifactId >springfox-swagger-ui</ artifactId > < version >2.4.0</ version > </ dependency > |
還需要在spring-mvc.xml中添加映射靜態(tài)的配置:
1
|
<mvc:default-servlet-handler /> |
然后就是swagger2的配置類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package com.xingguo.logistics.swagger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; 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 { @Bean public Docket buildDocket(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(buildApiInf()) .select() .apis(RequestHandlerSelectors.basePackage( "com.xingguo.logistics.controller" )) //controller路徑 .paths(PathSelectors.any()) .build(); } private ApiInfo buildApiInf(){ return new ApiInfoBuilder() .title( "xingguo大標題" ) .termsOfServiceUrl( "http://blog.csdn.net/u014231523網(wǎng)址鏈接" ) .description( "springmvc swagger2" ) .build(); } } |
然后運行項目,輸入自己的url。
http://{ip}:{port}/{projectname}/swagger-ui.html#/
我的url:
http://localhost:8989/logistics/swagger-ui.html#/
然后就可以看到效果圖:
它會把按照controller,把所有的接口都加載進來。
我的目錄結(jié)構(gòu)如圖:
然后,就是接口名稱和參數(shù)的說明:
常用注解:
- @Api()
用于類名
- @ApiOperation()
用于方法名
- @ApiParam()
用于參數(shù)說明
- @ApiModel()
用于實體類
- @ApiModelProperty
用于實體類屬性
更詳細的說明請參見官方注解說明文檔
使用方法如圖:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@Controller //類上使用@Api @Api (value= "用戶controller" ,description= "用戶相關(guān)操作" ) public class UserController { @RequestMapping (value= "index" ,method=RequestMethod.POST) //方法上使用@ApiOperation @ApiOperation (value= "首頁" ,notes= "跳轉(zhuǎn)到首頁" ) //參數(shù)使用@ApiParam public Object getIndex( @ApiParam (name= "topic實體" ,value= "json格式" ,required= true ) @RequestBody Topic topic){ //業(yè)務內(nèi)容,被我刪除了,請忽略,主要看上面的注解 Object obj = new Object(); return obj; } } |
1
2
3
4
5
|
//一般添加個@ApiModel()就可以,看情況使用里面的屬性 @ApiModel (value= "Topic" , discriminator = "foo" , subTypes = {Topic. class }) public class Topic{ } |
效果圖如下:
我在springboot中也集成了swagger2,集成方式基本相同,使用方式也基本一樣。請參考Spring Boot集成springfox-swagger2構(gòu)建restful API的方法教程
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如有疑問大家可以留言交流,謝謝大家對服務器之家的支持。