首页 > 其他分享 >Swagger使用案例

Swagger使用案例

时间:2024-06-22 12:01:44浏览次数:9  
标签:Swagger return value swagger 案例 使用 import springfox id

1、新建SpringBoot项目,导入swagger依赖

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

2、编写swagger的配置文件

import org.springframework.beans.factory.annotation.Value;
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;
 
/**
 1. swagger配置类
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //是否开启 (true 开启  false隐藏。生产环境建议隐藏)
                //.enable(false)
                .select()
                //扫描的路径包,设置basePackage会将包下的所有被@Api标记类的所有方法作为api
                .apis(RequestHandlerSelectors.basePackage("com.mcy.springbootswagger.controller"))
                //指定路径处理PathSelectors.any()代表所有的路径
                .paths(PathSelectors.any())
                .build();
    }
 
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //设置文档标题(API名称)
                .title("SpringBoot中使用Swagger2接口规范")
                //文档描述
                .description("接口说明")
                //服务条款URL
                .termsOfServiceUrl("http://localhost:8080/")
                //版本号
                .version("1.0.0")
                .build();
    }
}

3.@EnableSwagger2的作用是启用Swagger2相关功能

import com.mcy.springbootswagger.User.User;
import com.mcy.springbootswagger.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
@RestController
@RequestMapping("/user")
//说明接口文件
@Api(value = "测试接口", tags = "用户管理相关的接口", description = "用户测试接口")
public class UserController {
    @Autowired
    private UserService userService;
 
    /**
     * 保存数据
     * @param user
     * @return
     */
    @PostMapping(value = "/save")
    //方法参数说明,name参数名;value参数说明,备注;dataType参数类型;required 是否必传;defaultValue 默认值
    @ApiImplicitParam(name = "user", value = "新增用户数据")
    //说明是什么方法(可以理解为方法注释)
    @ApiOperation(value = "添加用户", notes = "添加用户")
    public String saveUser(User user){
        userService.save(user);
        return "保存成功";
    }
 
    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @GetMapping(value = "findById")
    @ApiOperation(value = "根据id获取用户信息", notes = "根据id查询用户信息")
    public User getUser(Integer id){
        return userService.findById(id);
    }
 
    @DeleteMapping(value = "deleteById")
    @ApiOperation(value = "根据id删除数据", notes = "删除用户")
    public String delete(Integer id){
        userService.deleteById(id);
        return "删除成功";
    }
}

运行项目,输入http://localhost:8080/swagger-ui.html访问Swagger页面,页面如下:

 Swagger使用的注解及其说明:

@Api用在类上,说明该类的作用
@ApiOperation注解来给API增加方法说明
@ApiParam定义在参数上
@ApiResponses用于表示一组响应
@ApiResponse用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类
@ApiModel

描述一个Model的信息(一般用在请求参数无法使用@ApiImplicitParam注解进行描述的时候)

@ApiModelProperty:描述一个model的属性

@ApiImplicitParams用在方法上包含一组参数说明
@ApiImplicitParam用来注解来给方法入参增加说明

标签:Swagger,return,value,swagger,案例,使用,import,springfox,id
From: https://blog.csdn.net/weixin_63164764/article/details/139879152

相关文章

  • 如何使用xlsx和file-saver插件实现导入导出
    首先,安装xlsx和file-saver插件在组件中引入import*asXLSXfrom'xlsx';importFileSaverfrom'file-saver';<div>  <!--导入表格-->  <labelfor="import-excel">导入表格:</label>  <inputid="import-excel&qu......
  • vscode中使用gitee
    1.下载安装githttps://www.cnblogs.com/htj10/p/12436737.html2.生成ssh密钥和公钥运行git-bash.exe,或 右键打开GitBashHere检查我们的目录下是否已经有公钥和私钥(如下是有的)#进入用户的目录john是自己的用户名$cd/c/users/john#查看是否有公钥John@DESKTOP-I......
  • 如何使用SQL工具批量执行SQL文件?(以MySQL和SQLynx为例)
    目录1.配置MySQL数据源2.打开SQL文件3.执行SQL文件4.检查执行结果5.SQL文件示例6.注意事项7.总结在现代数据库管理和操作中,批量执行SQL文件在MySQL中显现出其巨大的价值和不可替代的作用。通过将多个SQL语句集成在一个文件中进行批量处理,数据库管理......
  • 使用粒子滤波(particle filter)进行视频目标跟踪
    虽然有许多用于目标跟踪的算法,包括较新的基于深度学习的算法,但对于这项任务,粒子滤波仍然是一个有趣的算法。所以在这篇文章中,我们将介绍视频中的目标跟踪:预测下一帧中物体的位置。在粒子滤波以及许多其他经典跟踪算法的情况下,我们根据估计的动态进行预测,然后使用一些测量值更新预......
  • 已安装yarn,cmd可以使用,vscode中却无法识别
    使用VScode的终端时,全局安装的插件无法使用,如:yarn、cnpm、vue/cli、live-server、umi等。 解决方法:1.以管理员身份打开vscode2.在vscode终端执行get-ExecutionPolicy ===》查看结果为Restricted3.执行set-ExecutionPolicyRemoteSigned  ===》将ExecutionPolicy设......
  • Midas Civil2022安装使用教程
    MidasCivil是一款先进的桥梁与土木工程结构分析设计软件,专为桥梁工程师打造。它集成了强大有限元分析引擎,支持从初步设计到详细设计全过程,提供桥梁结构静力分析、动力分析、稳定性评估、抗震设计等功能。用户通过直观界面,能够高效建模、仿真各类复杂工况,确保结构安全性与经济......
  • SpringBoot 项目使用 Mybatis Plus 实现多租户
    pom文件<properties><mybatis-plus.version>3.5.1</mybatis-plus.version></properties><!--mybatis-plus依赖配置--><dependency><groupId>com.baomidou</groupId><artifactId&g......
  • 316 API Versions 03(Enabling API Versions in Swagger)
    更新Program.cs添加两个版本的SwaggerDoc//Swaggerbuilder.Services.AddEndpointsApiExplorer();//generatesdescriptionforallendpointsbuilder.Services.AddSwaggerGen(options=>{options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory,"api.x......
  • LOMBOK使用详解
    最近正在写SpringBoot系列文章和录制视频教程,每次都要重复写一些Getter/Setter、构造器方法、字符串输出的ToString方法和Equals/HashCode方法等。甚是浪费时间,也影响代码的可读性。因此,今天就给大家推荐一款Java开发神器——Lombok,让代码更简单易读。什么是LombokLombok是一款J......
  • java集合使用中的注意事项
    集合判断是否为空判断所有集合内部的元素是否为空,使用 isEmpty() 方法,而不是 size()==0 的方式这是因为isEmpty()方法的可读性更好,并且时间复杂度为O(1)。绝大部分我们使用的集合的size()方法的时间复杂度也是O(1),不过,也有很多复杂度不是O(1)的,比如java.util.c......