SpringBoot基于Swagger2构建API文档过程解析

 更新时间:2019年11月06日 08:33:48   作者:我好难啊upup  
这篇文章主要介绍了SpringBoot基于Swagger2构建API文档过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

一、添加依赖

<!--SpringBoot使用Swagger2构建API文档的依赖-->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.7.0</version>
    </dependency>

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

二、创建Swagger2配置类

package com.offcn.config;

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;

@Configuration//表示该类为一个配置类,相当于spring中的xml配置文件
@EnableSwagger2 //开启在线文档
public class SwaggerConfig {

  //1.声明 api 文档的属性
  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .title("Spring Boot中使用Swagger2构建RESTful APIs")
        .description("优就业")
        .termsOfServiceUrl("http://www.ujiuye.com/")
        .contact("小刘同学")
        .version("1.0")
        .build();
  }

  //配置核心配置信息
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.offcn.controller"))
        .paths(PathSelectors.any())
        .build();
  }
}

三、修改Controller 增加文档注释

通过@ApiOperation注解来给API增加说明

通过@ApiImplicitParams@ApiImplicitParam注解来给参数增加说明

package com.offcn.controller;

import com.offcn.dao.UserDao;
import com.offcn.entity.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/rest")
@RestController
public class RestFulController {

  @Autowired
  private UserDao userDao;

  @GetMapping("/getUserById")
  @ApiOperation(value="查找指定id用户信息", notes="根据id查找用户信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer"),
  })
  public User getUserById(Integer id){
    User user = userDao.getOne(id);
    return user;
  }

  @DeleteMapping("/del")
  @ApiOperation(value="删除指定id用户信息", notes="根据id删除用户信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer"),
  })
  public String delUserById(Integer id){
    userDao.deleteById(id);
    return "success";
  }
}

四、查看Swagger2文档

重启项目

访问:

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • SpringBoot查询数据库导出报表文件方式

    SpringBoot查询数据库导出报表文件方式

    这篇文章主要介绍了SpringBoot查询数据库导出报表文件方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-04-04
  • 在SpringBoot接口中正确地序列化时间字段的方法

    在SpringBoot接口中正确地序列化时间字段的方法

    文章主要介绍在 Spring Boot 接口中正确序列化时间字段的方法,包括 Java 中Date和LocalDateTime类型的区别,JSON 序列化和请求参数中时间字段的处理,如时间字符串的格式配置、时间戳的使用及相关配置,还提到了在 Swagger UI 中的类型设置,需要的朋友可以参考下
    2024-11-11
  • JDK10新特性之var泛型和多个接口实现方法

    JDK10新特性之var泛型和多个接口实现方法

    这篇文章主要介绍了JDK10的新特性:var泛型和多个接口实现方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-05-05
  • Java微信跳一跳操作指南

    Java微信跳一跳操作指南

    这篇文章主要为大家详细介绍了Java微信跳一跳操作指南,通过adb来控制手机进行操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • java 对象数组排序

    java 对象数组排序

    当遇到数组排序时,我们经常会使用学过的几种排序方法,而java 本身提供了Arrays.sort,在数据元素较少或者对效率要求不是抬高时,直接使用Arrays.sort来的更容易。查看一下源码后Arrays.sort 本身采用的是快速排序。
    2015-04-04
  • springBoot+webMagic实现网站爬虫的实例代码

    springBoot+webMagic实现网站爬虫的实例代码

    这篇文章主要介绍了springBoot+webMagic实现网站爬虫的实例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • SpringBoot+MinIO+KKFileView实现文件预览功能

    SpringBoot+MinIO+KKFileView实现文件预览功能

    本文主要介绍了使用SpringBoot、MinIO和KKFileView实现文件上传和在线预览功能,通过配置MinIO存储文件,并使用KKFileView生成预览链接,感兴趣的可以了解一下
    2024-11-11
  • IDEA上实现JDBC编程的方法步骤

    IDEA上实现JDBC编程的方法步骤

    本文主要介绍了IDEA上实现JDBC编程的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • 解读String字符串拼接的原理

    解读String字符串拼接的原理

    这篇文章主要介绍了关于String字符串拼接的原理,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • 深度解析Java中的国际化底层类ResourceBundle

    深度解析Java中的国际化底层类ResourceBundle

    做项目应该都会实现国际化,那么大家知道Java底层是如何实现国际化的吗?这篇文章就来和大家深度解析一下Java中的国际化底层类ResourceBundle,希望对大家有所帮助
    2023-03-03

最新评论