前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot整合swagger

springboot整合swagger

作者头像
九转成圣
发布2024-04-10 17:35:45
1060
发布2024-04-10 17:35:45
举报
文章被收录于专栏:csdncsdn

springboot整合swagger

整合swagger2

依赖

代码语言:javascript
复制
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

配置swagger

代码语言:javascript
复制
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2 {
    //swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //为当前包路径
                .apis(RequestHandlerSelectors.basePackage("com.example.hello.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("Spring Boot 测试使用 Swagger2 构建RESTful API")
                //创建人
                .contact(new Contact("cookie", "http://www.baidu.com", "2118119173@qq.com"))
                //版本号
                .version("1.0")
                //描述
                .description("用户管理")
                .build();
    }

}

访问

http://localhost:8080/swagger-ui.html

整合swagger3

依赖

代码语言:javascript
复制
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

配置

同swagger2

访问

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

Swagger常用注解

@Api:修饰整个类,描述Controller的作用 @ApiOperation:描述一个类的一个方法,或者说一个接口 @ApiParam:单个对象参数描述

代码语言:javascript
复制
@ApiResponse(code = 200, message = "成功,返回用户信息",response = Dept.class,responseContainer = "List")
@ApiParam(value = "用户信息")
@PostMapping("/getDeptListByPage")
public MyResponse<List<Dept>> selectList( @RequestBody Dept param) {
    List<Dept> depts = deptService.selectList(param);
    return MyResponse.success(depts);
}

@ApiModel:用对象来接收参数

@ApiModelProperty

代码语言:javascript
复制
@ApiModel("部门实体类")
public class Dept extends BasicEntity {

   /**
    * 部门编号
    */
   @ApiModelProperty(value = "部门编号",example = "100")
   private Integer deptno;

   /**
    * 部门名称
    */
   @ApiModelProperty("部门名称")
   private String dname;

   /**
    * 部门所在地
    */
   @ApiModelProperty("部门所在地")
   private String loc;

}

@ApiResponse:HTTP响应其中1个描述

代码语言:javascript
复制
@ApiResponse(code = 200, message = "成功,返回用户信息",response = Dept.class,responseContainer = "List")
@ApiParam(value = "用户信息")
@PostMapping("/getDeptListByPage")
public MyResponse<List<Dept>> selectList( @RequestBody Dept param) {
    List<Dept> depts = deptService.selectList(param);
    return MyResponse.success(depts);
}

@ApiParamImplicitL:一个请求参数 @ApiParamsImplicit 多个请求参数

代码语言:javascript
复制
@ApiOperation("根据地址和名字查询部门")
@ApiImplicitParams({
    @ApiImplicitParam(name = "dname", value = "部门名称", example = "开发部"),
    @ApiImplicitParam(name = "loc", value = "部门地址", example = "北京")
})
@DeleteMapping("/getDept")
public MyResponse getDept(String dname, String loc) {

    return MyResponse.success();
}

@ApiParam与@ApiImplicitParam

基本类型用@ApiParam不生效??

代码语言:javascript
复制
@ApiOperation("根据主键删除")
@ApiParam(name = "id", value = "部门编号", example = "100")
// @ApiImplicitParam(name = "id", value = "部门编号", example = "100")
@DeleteMapping("/dept/{id}")
public MyResponse deleteByPrimaryKey(@PathVariable("id") Integer id) {
    deptService.deleteByPrimaryKey(id);
    return MyResponse.success().setMessage("删除成功");
}
代码语言:javascript
复制
@ApiOperation("根据主键删除")
@ApiParam(name = "id", value = "部门编号", example = "100")
// @ApiImplicitParam(name = "id", value = "部门编号", example = "100")
@DeleteMapping("/dept2")
public MyResponse deleteByPrimaryKey2(Integer id) {
    deptService.deleteByPrimaryKey(id);
    return MyResponse.success().setMessage("删除成功");
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-04-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • springboot整合swagger
  • 整合swagger2
    • 依赖
      • 配置swagger
        • 访问
        • 整合swagger3
          • 依赖
            • 配置
              • 访问
              • Swagger常用注解
              • @ApiParam与@ApiImplicitParam
              相关产品与服务
              Serverless HTTP 服务
              Serverless HTTP 服务基于腾讯云 API 网关 和 Web Cloud Function(以下简称“Web Function”)建站云函数(云函数的一种类型)的产品能力,可以支持各种类型的 HTTP 服务开发,实现了 Serverless 与 Web 服务最优雅的结合。用户可以快速构建 Web 原生框架,把本地的 Express、Koa、Nextjs、Nuxtjs 等框架项目快速迁移到云端,同时也支持 Wordpress、Discuz Q 等现有应用模版一键快速创建。
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档