spring boot 之 swagger2 api 文档用法

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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小噻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值