1.在pom.xml 中添加依赖包
<!-- 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>
2.在项目目录下添加swagger2 配置类
package com.von.audit.config;
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;
/**
* @author xiaosai
* @version 1.0
* @date 2020/6/6 9:54
* @describe SWAGGER2配置类
* @className
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.von.audit.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo getApiInfo(){
return new ApiInfoBuilder()
.title("审计API")
.description("经济、项目、财产等账目记录")
.version("1.0")
.license("Apache 2.0")
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")
.build();
}
}
3.启动项目,访问:ip:port/swagger-ui.html
4.主要注解的使用
@EnableSwagger2
启用swagger
@Api(value = “登录、验证、注册”,tags = “登录、验证、注册”)
@ApiOperation(value = “登录接口”,notes = “登录接口”)
@ApiImplicitParams({
@ApiImplicitParam(value = “”,dataType = “”)
})
@ApiResponses({
@ApiResponse(code = 200,message = “”),
@ApiResponse(code = 404,message = “”)
})
package com.von.audit.controller;
import com.von.audit.dao.TbAuditUser;
import com.von.audit.dao.TbAuditUserKey;
import com.von.audit.po.AuditUserParams;
import com.von.audit.service.AuditLoginService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author xiaosai
* @version 1.0
* @date 2020/6/5 16:35
* @describe 登录、验证、注册
* @className
*/
@Controller
@RequestMapping("/audit")
@Api(value = "登录、验证、注册",tags = "登录、验证、注册")
public class AuditLoginController {
@Autowired
private AuditLoginService auditLoginService;
@RequestMapping(value = "/login",method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "登录接口",notes = "登录接口")
@ApiImplicitParams({
@ApiImplicitParam(value = "",dataType = "")
})
@ApiResponses({
@ApiResponse(code = 200,message = ""),
@ApiResponse(code = 404,message = "")
})
public TbAuditUser login(){
TbAuditUser auditUser = auditLoginService.getAuditUser(new TbAuditUserKey() {{
setId(1);
setUserName("1");
}});
return auditUser;
}
@RequestMapping(value = "/register",method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "注册接口",notes = "注册接口")
public String register(@RequestBody AuditUserParams auditUserParams){
return "success";
}
@RequestMapping(value = "/check",method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "验证接口",notes = "验证接口")
public String check(@RequestBody AuditUserParams auditUserParams){
return "success";
}
}
5.实体类注解
@ApiModel(value = “注册请求参数”)
@ApiModelProperty(value = “id”)
package com.von.audit.po;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.Date;
/**
* @author xiaosai
* @version 1.0
* @date 2020/6/5 17:46
* @describe 注册请求参数
* @className
*/
@ApiModel(value = "注册请求参数")
public class AuditUserParams {
@ApiModelProperty(value = "id")
private Integer id;
@ApiModelProperty(value = "用户名")
private String userName;
@ApiModelProperty(value = "uuid")
private String userId;
@ApiModelProperty(value = "logo图标id")
private Integer logoIconId;
@ApiModelProperty(value = "部门id")
private Integer deptId;
@ApiModelProperty(value = "是否为超级管理员 1-是")
private Boolean isSuperAdmin;
@ApiModelProperty(value = "姓名")
private String name;
@ApiModelProperty(value = "密码")
private String password;
@ApiModelProperty(value = "年龄")
private Integer age;
@ApiModelProperty(value = "性别:1-男,2-女")
private Integer gender;
@ApiModelProperty(value = "电话号码")
private String telephone;
@ApiModelProperty(value = "邮箱")
private String mail;
@ApiModelProperty(value = "身份证号")
private String idNum;
@ApiModelProperty(value = "备注")
private String remark;
@ApiModelProperty(value = "用户名")
private Date createTime;
@ApiModelProperty(value = "用户名")
private Date modifyTime;
@ApiModelProperty(value = "地址")
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public Integer getLogoIconId() {
return logoIconId;
}
public void setLogoIconId(Integer logoIconId) {
this.logoIconId = logoIconId;
}
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
public Boolean getSuperAdmin() {
return isSuperAdmin;
}
public void setSuperAdmin(Boolean superAdmin) {
isSuperAdmin = superAdmin;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getIdNum() {
return idNum;
}
public void setIdNum(String idNum) {
this.idNum = idNum;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getModifyTime() {
return modifyTime;
}
public void setModifyTime(Date modifyTime) {
this.modifyTime = modifyTime;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}