創建springboot就不說了很簡單
在pom中添加swagger2依賴
<!--swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
接下來寫Swagger2的配置文件
此處我創建了兩個模塊在Select a spec選擇的。
此處一定要修改掃描包路徑
package com.allmodel.models.config;
/**
* @Author WQY
* @Date 2019/10/19 10:29
* @Version 1.0
*/
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Swagger2的配置類
* Created by 30721 on 2019/7/7.
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean("Authority")
public Docket apiAuthority() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("權限模塊接口")
.apiInfo(apiInfo())
.select()
// 自行修改爲自己的包路徑
.apis(RequestHandlerSelectors.basePackage("com.allmodel.models.authority.controller"))
.paths(PathSelectors.any())
.build();
}
@Bean("Test")
public Docket apiTest() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("測試模塊接口")
.apiInfo(apiInfo())
.select()
// 自行修改爲自己的包路徑
.apis(RequestHandlerSelectors.basePackage("com.allmodel.models.test.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("模塊接口")
.description("")//swagger UI接入教程
//服務條款網
.termsOfServiceUrl("")
//版本號
.version("1.0")
.build();
}
}
然後在controller和實體類種添加註解,形成接口說明
比如用戶管理的這個接口
- @Api:用在請求的類上,說明該類的作用
- @ApiOperation:用在請求的方法上,說明方法的作用
- @ApiImplicitParams:用在請求的方法上,包含一組參數說明
- @ApiResponses:用於請求的方法上,表示一組響應
- @ApiModel:用於響應類上,表示一個返回響應數據的信息
@Controller
@RequestMapping("/user")
@Api(value = "用戶組織機構管理")
public class UserManagement {
@Autowired
private UserManagementService userManagementService;
/**
* 添加用戶
* @param userEntity
* @return
*/
@RequestMapping(value = "/adduser",method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "添加用戶")
public OutView<String> addUser(@ApiParam(name = "userEntity",value = "傳入json格式",required = true) UserEntity userEntity){
String rs = userManagementService.addUser(userEntity);
OutView outView = new OutView();
try {
outView.setState(0);
outView.setMsg(rs);
return outView;
}catch (Exception e){
outView.setState(1);
outView.setMsg(rs);
return outView;
}
}
/**
* 獲取用戶列表
* @param orgnum
* @return
*/
@RequestMapping(value = "/getorglist",method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "獲取用戶列表")
@ApiImplicitParam(value = "組織機構編碼",name = "orgnum",dataType = "String",required = true)
public OutView<List<UserEntity>> getOrgList(String orgnum){
OutView outView = new OutView();
try {
List<UserEntity> rs = userManagementService.getuserlist(orgnum+"%");
outView.setState(0);
outView.setMsg(rs);
return outView;
}catch (Exception e){
outView.setState(1);
outView.setMsg("獲取失敗");
return outView;
}
}
}
用法介紹
@Api:用在請求的類上,表示對類的說明
tags="說明該類的作用,可以在UI界面上看到的註解"
value="該參數沒什麼意義,在UI界面上也看到,所以不需要配置"
@ApiOperation:用在請求的方法上,說明方法的用途、作用
value="說明方法的用途、作用"
notes="方法的備註說明"
@ApiImplicitParams:用在請求的方法上,表示一組參數說明
@ApiImplicitParam:用在@ApiImplicitParams註解中,指定一個請求參數的各個方面
name:參數名
value:參數的漢字說明、解釋
required:參數是否必須傳
paramType:參數放在哪個地方
· header --> 請求參數的獲取:@RequestHeader
· query --> 請求參數的獲取:@RequestParam
· path(用於restful接口)--> 請求參數的獲取:@PathVariable
· body(不常用)
· form(不常用)
dataType:參數類型,默認String,其它值dataType="Integer"
defaultValue:參數的默認值
@ApiResponses:用在請求的方法上,表示一組響應
@ApiResponse:用在@ApiResponses中,一般用於表達一個錯誤的響應信息
code:數字,例如400
message:信息,例如"請求參數沒填好"
response:拋出異常的類
@ApiModel:用於響應類上,表示一個返回響應數據的信息
(這種一般用在post創建的時候,使用@RequestBody這樣的場景,
請求參數無法使用@ApiImplicitParam註解進行描述的時候)
@ApiModelProperty:用在屬性上,描述響應類的屬性
在剛纔上面代碼中有個返回類型爲
OutView<List<UserEntity>>
的接口。
此處如果想要對方看見字段的解釋一定要在實體類中加上泛型,不然是看不見你在UserEntity中做的註解
由於我項目中用的是JPA,所以請忽略JPA註解即可
就是忽略非@Api開口的註解
package com.allmodel.models.authority.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import javax.persistence.*;
import java.util.Date;
/**
* @Description
* @Author WQY
* @Date 2019-11-05
*/
@Entity
@Table ( name ="user" )
@ApiModel(value = "user",description = "用戶對象user")
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id" )
@ApiModelProperty(hidden = true)
private Long id;
/**
* 用戶名
*/
@Column(name = "username" )
@ApiModelProperty(value = "用戶名",name = "username",required = true,dataType = "String")
private String username;
/**
* 密碼
*/
@Column(name = "password" )
@ApiModelProperty(value = "密碼",name = "password",required = true,dataType = "String")
private String password;
/**
* 保存時間
*/
@Column(name = "savetime" )
@ApiModelProperty(hidden = true)
private Date savetime;
/**
* 手機號
*/
@Column(name = "iphone" )
@ApiModelProperty(value = "手機號",name = "iphone")
private String iphone;
/**
* 權限,1是最大權限,2是最小權限
*/
@Column(name = "authority" )
@ApiModelProperty(value = "權限",name = "authority")
private Long authority;
/**
* 性別,1男,2女
*/
@Column(name = "sex" )
@ApiModelProperty(value = "性別",name = "sex")
private String sex;
/**
* 組織機構編號
*/
@Column(name = "organization_num" )
@ApiModelProperty(value = "組織機構標號",name = "organizationNum",required = true,dataType = "String")
private String organizationNum;
/**
* 組織機構名稱
*/
@Column(name = "organization_name" )
@ApiModelProperty(value = "組織機構名稱",name = "organizationName",required = true,dataType = "String")
private String organizationName;
}
然後訪問http://localhost:端口/swagger-ui.html
就可以看到頁面了
箭頭指的就是剛纔添加的兩個接口
因爲這兩個接口是在權限模塊接口下,所以Select a spec選的是權限模塊接口