全部
常见问题
产品动态
精选推荐

swagger的使用与步骤

管理 管理 编辑 删除

1、导入maven工程

首先我们创建一个 Spring Boot 项目,并引入 Swagger3 的核心依赖包,如下:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

2、核心配置

接下来我们在启动类上添加两个注解,开启Swagger功能。

//开启swagger
@EnableSwagger2
@EnableOpenApi
@SpringBootApplication
public class SwaggerApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(SwaggerApplication.class, args);
    }
 
}

3、启动项目

接下来让我们可以启动项目,然后在浏览器中输入如下地址:

http://localhost:8085/swagger-ui/index.html

注意,端口是自己tomcat启动时的端口,以自己电脑的为准

4、进入界面

e7fb8202305051809275262.png

5、swagger配置类

package com.swagger.config;
 
import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
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;
 
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).groupName("用户组")
            .select() .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
            .paths(PathSelectors.ant("/swagger/**"))
            .build();
    }
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
            .title("Api接口文档")
            .description("API描述")
            .version("1.0") .termsOfServiceUrl("https://www.baidu.com") .build(); }
 
 
}

6、Controller接口配置

@Api(tags = "用户控制")代表对这个controller的描述

4204b202305051809574129.png

@ApiOperation(value = "查询所有用户", notes = "查询所有用户信息")代表对接口的描述

2b376202305051810094583.png

package com.swagger.controller;
 
import com.baomidou.mybatisplus.extension.api.R;
import com.swagger.domain.User;
import com.swagger.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.util.List;
 
@Api(tags = "用户控制")
@RestController
@RequestMapping("/swagger/user")
@CrossOrigin
@Slf4j
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @GetMapping("/selectAll")
    @ResponseBody
    @ApiOperation(value = "查询所有用户", notes = "查询所有用户信息")
    public R selectAll(){
        List<User> list = userService.list();
        System.out.println(list);
        return R.ok(list).setCode(200);
    }
    @PostMapping("/save")
    @ApiOperation(value = "新增用户", notes = "新增用户信息")
    public R save(@RequestBody User user){
        return R.ok("success").setCode(200);
    }
 
    @PutMapping("/update")
    @ApiOperation(value = "修改用户", notes = "修改用户信息")
    public R update(@RequestBody User user){
        return R.ok("success").setCode(200);
    }
 
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除用户", notes = "删除用户信息")
    public R delete(int id){
        return R.ok("success").setCode(200);
    }
 
}

7、实体类配置

@ApiModel属性:description:用于描述实体类

@ApiModel(value = "用户实体",description = "用户实体")

8308d202305051810329155.png

@ApiModelProperty属性:notes:描述该实体类属性的信息

@ApiModelProperty(notes = "用户Id")

代码演示

package com.swagger.domain;
 
import com.baomidou.mybatisplus.annotation.*;
 
import java.io.Serializable;
import java.util.Date;
 
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
 
/**
 * 用户
 * @TableName pd_auth_user
 */
@TableName(value ="pd_auth_user")
@Data
@ApiModel(value = "用户实体",description = "用户实体")
public class User implements Serializable {
    /**
     * ID
     */
    @ApiModelProperty(notes = "用户Id")
    @TableId(value = "id")
    private Long id;
 
    /**
     * 账号
     */
    @ApiModelProperty(notes = "账号")
    @TableField(value = "account")
    private String account;
 
    /**
     * 姓名
     */
    @ApiModelProperty(notes = "姓名")
    @TableField(value = "name")
    private String name;
 
    /**
     * 组织ID
#c_core_org
     */
    @ApiModelProperty(notes = "组织ID")
    @TableField(value = "org_id")
    private Long org_id;
 
    /**
     * 岗位ID
#c_core_station
     */
    @ApiModelProperty(notes = "岗位ID")
    @TableField(value = "station_id")
    private Long station_id;
 
    /**
     * 邮箱
     */
    @ApiModelProperty(notes = "邮箱")
    @TableField(value = "email")
    private String email;
 
    /**
     * 手机
     */
    @ApiModelProperty(notes = "手机")
    @TableField(value = "mobile")
    private String mobile;
 
    /**
     * 性别
#Sex{W:女;M:男;N:未知}
     */
    @ApiModelProperty(notes = "性别   W:女;M:男;N:未知")
    @TableField(value = "sex")
    private String sex;
 
    /**
     * 启用状态 1启用 0禁用
     */
    @ApiModelProperty(notes = "启用状态 1启用 0禁用")
    @TableField(value = "status")
    private Boolean status;
 
    /**
     * 头像
     */
    @ApiModelProperty(notes = "头像")
    @TableField(value = "avatar")
    private String avatar;
 
    /**
     * 工作描述
比如:  市长、管理员、局长等等   用于登陆展示
     */
    @ApiModelProperty(notes = "工作描述\n" +
        "比如:  市长、管理员、局长等等   用于登陆展示")
    @TableField(value = "work_describe")
    private String work_describe;
 
    /**
     * 最后一次输错密码时间
     */
    @ApiModelProperty(notes = "最后一次输错密码时间")
    @TableField(value = "password_error_last_time")
    private Date password_error_last_time;
 
    /**
     * 密码错误次数
     */
    @ApiModelProperty(notes = "密码错误次数")
    @TableField(value = "password_error_num")
    private Integer password_error_num;
 
    /**
     * 密码过期时间
     */
    @ApiModelProperty(notes = "密码过期时间")
    @TableField(value = "password_expire_time")
    private Date password_expire_time;
 
    /**
     * 密码
     */
    @ApiModelProperty(notes = "密码")
    @TableField(value = "password")
    private String password;
 
    /**
     * 最后登录时间
     */
    @ApiModelProperty(notes = "最后登录时间")
    @TableField(value = "last_login_time")
    private Date last_login_time;
 
    /**
     * 创建人id
     */
    @ApiModelProperty(notes = "创建人id")
    @TableField(value = "create_user")
    private Long create_user;
 
    /**
     * 创建时间
     */
    @ApiModelProperty(notes = "创建时间")
    @TableField(value = "create_time",fill = FieldFill.INSERT)
    private Date create_time;
 
    /**
     * 更新人id
     */
    @ApiModelProperty(notes = "更新人id")
    @TableField(value = "update_user")
    private Long update_user;
 
    /**
     * 更新时间
     */
    @ApiModelProperty(notes = "更新时间")
    @TableField(value = "update_time",fill = FieldFill.INSERT_UPDATE)
    private Date update_time;
 
    @TableField(exist = false)
    private static final long serialVersionUID = 1L;
}

swagger显示

5570d202305051811323410.png

8、注解

781952023050518121325.png


请登录后查看

CRMEB-慕白寒窗雪 最后编辑于2023-05-05 18:12:30

快捷回复
回复
回复
回复({{post_count}}) {{!is_user ? '我的回复' :'全部回复'}}
排序 默认正序 回复倒序 点赞倒序

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level }}

作者 管理员 企业

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推荐': '推荐'}}
{{item.is_suggest == 1? '取消推荐': '推荐'}}
沙发 板凳 地板 {{item.floor}}#
{{item.user_info.title || '暂无简介'}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
{{item.like_count}}
{{item.showReply ? '取消回复' : '回复'}}
删除
回复
回复

{{itemc.user_info.nickname}}

{{itemc.user_name}}

回复 {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回复' : '回复'}}
删除
回复
回复
查看更多
1184
{{like_count}}
{{collect_count}}
添加回复 ({{post_count}})

快速安全登录

使用微信扫码登录
{{item.label}} 加精
{{item.label}} {{item.label}} 板块推荐 常见问题 产品动态 精选推荐 首页头条 首页动态 首页推荐
取 消 确 定
回复
回复
问题:
问题自动获取的帖子内容,不准确时需要手动修改. [获取答案]
答案:
提交
bug 需求 取 消 确 定

微信登录/注册

切换手机号登录

{{ bind_phone ? '绑定手机' : '手机登录'}}

{{codeText}}
切换微信登录/注册
暂不绑定
CRMEB客服

CRMEB咨询热线 咨询热线

400-8888-794

微信扫码咨询

CRMEB开源商城下载 源码下载 CRMEB帮助文档 帮助文档
返回顶部 返回顶部
CRMEB客服