1. 简介

  随着前后端分离开发模式越来越流行,编写接口文档变成了开发人员非常头疼的事。而Swagger是一个规范且完整的web框架,用于生成、描述、调用可视化的RESTful风格的在线接口文档,并解决手写文档时编写和更新以及测试的复杂问题。

2. 示例代码

  • 创建项目

  • 修改pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.c3stones</groupId>
<artifactId>spring-boot-swagger2-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-swagger2-demo</name>
<description>Spring Boot Swagger2 Demo</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.8.RELEASE</version>
<relativePath />
</parent> <dependencies>
<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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </project>
  • 创建Swagger2配置类
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; /**
* Swagger2配置类
*
* @author CL
*
*/
@Configuration
@EnableSwagger2
public class Swagger2Config { /**
* 注入Docket
*
* @return
*/
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(setApiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.c3stones.controller")).paths(PathSelectors.any())
.build();
} /**
* 配置在线文档的基本信息
*
* @return
*/
private ApiInfo setApiInfo() {
return new ApiInfoBuilder().title("SpringBoot整合Swagger2示例")
.description("<a href='https://www.cnblogs.com/cao-lei/' target='_blank'>欢迎访问我的博客</a>")
.termsOfServiceUrl("https://www.cnblogs.com/cao-lei/").version("V1.0").build();
} }
  • 创建实体
import lombok.AllArgsConstructor;
import lombok.Data; /**
* 用户信息
*
* @author CL
*
*/
@Data
@AllArgsConstructor
public class User { /**
* 用户ID
*/
private Long id; /**
* 用户名称
*/
private String username; /**
* 年龄
*/
private Integer age; }
  • 创建Controller
import java.util.ArrayList;
import java.util.List; import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController; import com.c3stones.entity.User; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore; /**
* 用户Controller
*
* @author CL
*
*/
@Api(tags = "User / 用户信息")
@RestController
@RequestMapping(value = "/user")
public class UserController { /**
* 获取用户信息
*
* @param user 用户信息
* @return
*/
@RequestMapping(value = "/get", method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "获取用户信息")
@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户ID", required = true, type = "Long"),
@ApiImplicitParam(name = "username", value = "用户名称", type = "String"),
@ApiImplicitParam(name = "age", value = "年龄", type = "Integer") })
public User get(User user) {
return new User(1L, "C3Stones", 23);
} /**
* 获取用户列表
*
* @param user 用户信息
* @return
*/
@SuppressWarnings("serial")
@RequestMapping(value = "/listData", method = { RequestMethod.GET, RequestMethod.POST })
@ResponseBody
@ApiOperation(value = "获取用户列表")
@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户ID", type = "Long"),
@ApiImplicitParam(name = "username", value = "用户名称", type = "String"),
@ApiImplicitParam(name = "age", value = "年龄", type = "Integer") })
public List<User> listData(User user, @ApiIgnore HttpRequest request) {
return new ArrayList<User>() {
{
add(new User(1L, "张三", 23));
add(new User(2L, "李四", 24));
add(new User(3L, "王五", 25));
}
};
}
}
  • 创建启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* 启动类
*
* @author CL
*
*/
@SpringBootApplication
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }

3. 测试

  • 启动项目
  • 浏览器访问
http://127.0.0.1:8080/swagger-ui.html

4. 项目地址

  spring-boot-swagger2-demo

SpringBoot整合Swagger2详细教程的更多相关文章

  1. SpringBoot整合Swagger2

    相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人开发前后台的年代,当我没说,如今为了前后台更好的对接,还是为了以后交接方便,都有要求写API文档. 手写Api文档的几个痛点: 文档需 ...

  2. springboot 整合Swagger2的使用

    Swagger2相较于传统Api文档的优点 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时. 接口返回结果不明确 不能直接在线测试接口,通常需要使用工 ...

  3. SpringBoot整合Swagger2案例,以及报错:java.lang.NumberFormatException: For input string: ""原因和解决办法

    原文链接:https://blog.csdn.net/weixin_43724369/article/details/89341949 SpringBoot整合Swagger2案例 先说SpringB ...

  4. SpringBoot整合Swagger2及使用

    简介 swagger是一个流行的API开发框架,这个框架以"开放API声明"(OpenAPI Specification,OAS)为基础, 对整个API的开发周期都提供了相应的解决 ...

  5. Struts2+Spring4+Hibernate4整合超详细教程

    Struts2.Spring4.Hibernate4整合 超详细教程 Struts2.Spring4.Hibernate4整合实例-下载 项目目的: 整合使用最新版本的三大框架(即Struts2.Sp ...

  6. SpringBoot(七):SpringBoot整合Swagger2

    原文地址:https://blog.csdn.net/saytime/article/details/74937664 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文 ...

  7. SpringBoot整合Swagger2(Demo示例)

    写在前面 由于公司项目采用前后端分离,维护接口文档基本上是必不可少的工作.一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理 ...

  8. Springboot 整合 MyBatisPlus[详细过程]

    Springboot 整合 MyBatisPlus[详细过程] 提要 这里已经将Springboot环境创建好 这里只是整合MyBatis过程 引入Maven依赖 添加MyBatisPlus启动依赖, ...

  9. Spring Data JPA系列2:SpringBoot集成JPA详细教程,快速在项目中熟练使用JPA

    大家好,又见面了. 这是Spring Data JPA系列的第2篇,在上一篇<Spring Data JPA系列1:JDBC.ORM.JPA.Spring Data JPA,傻傻分不清楚?给你个 ...

随机推荐

  1. 为什么说线程太多,cpu切换线程会浪费很多时间?

    问题1: 假如有一个计算任务,计算1-100的和,每10个数相加,需要占用一个cpu时间片(1s).如果起一个线程(模拟没有线程切换),完成任务需要多长时间?如果起5个线程,完成任务需要消耗多久时间? ...

  2. 一个工作三年左右的Java程序员和大家谈谈从业心得

    转发链接地址:https://mp.weixin.qq.com/s/SSh9HcA5PgMHv7xiolQkig 貌似这一点适应的行业最广,但是我可以很肯定的说:当你从事web开发一年后,重新找工作时 ...

  3. JVM字节码执行引擎

    一.概述 在不同的虚拟机实现里面,执行引擎在执行Java代码的时候可能会有解释执行(通过解释器执行)和编译器执行(通过即时编译器产生本地代码执行)两种选择,所有的Java虚拟机的执行引擎都是一致的:输 ...

  4. tp5 跨域问题

    只需要三行代码,写到入口文件public/index.php处即可解决 header("Access-Control-Allow-Origin:*"); header(" ...

  5. 面试官:小伙子,你给我说一下Java中什么情况会导致内存泄漏呢?

    概念 内存泄露:指程序中动态分配内存给一些临时对象,但对象不会被GC回收,它始终占用内存,被分配的对象可达但已无用.即无用对象持续占有内存或无用对象的内存得不到及时释放,从而造成的内存空间浪费. 可达 ...

  6. 阿里面试官:小伙子,你给我说一下Spring Bean初始化的几种常规方式吧

    前言 通过构造方法实例化通过静态工厂实例化通过实例工厂实例化通过FactoryBean实例化 RumenzA实体类 package com.rumenz; public class RumenzA { ...

  7. Pinpoint 设置微信或者钉钉预警

    本文基于 Pinpoint 2.1.0 版本 本文大部分内容来自:侠梦的开发笔记 ,但是原文的版本和我的不一致,放在2.1.0是跑不起来的,但是大概逻辑和思路基本一致. 目录 一.接入预警大概思路 二 ...

  8. 通过PHPExcel将Excel表文件中数据导入数据库

    1 <?php 2 header("Content-Type:text/html;charset=utf-8"); 3 include_once 'PHPExcel.php' ...

  9. 关于UILabel标签控件的使用小节

    前段时间一直想停下来,总结一下近期在开发中遇到的一些问题顺便分享一下解决问题的思路和方法,无奈人生就像蒲公英,看似自由却身不由己.太多的时间和精力被占用在新项目的开发和之前项目的维护中,总之一句话外包 ...

  10. 抓包工具fiddler使用-初级

    参考 https://kb.cnblogs.com/page/130367/#introduce