Spring boot quickly integrates swagger

Jiang Chengjun 2021-11-25 19:04:26
spring boot quickly integrates swagger

One 、 Preface

Spring Boot As the most popular Java web Development scaffold , More and more developers choose to use it to build enterprise class RESTFul API Interface . These interfaces will not only serve the traditional web End (b/s), It will also serve the mobile terminal . In the actual development process , These interfaces should also be provided for development testing to carry out relevant white box testing , Then there must be how to share and update in time in multi person cooperation API Problems in developing interface documents .

Use Swagger Integration documents have the following advantages :

  • Rich in functions  : Support multiple annotations , Automatically generate interface document interface , Support in interface testing API Interface functions ;
  • update  : Take a little time to write notes in the development process , It can be updated in time API file , Save the mind and save labour ;
  • Simple integration  : By adding pom Dependency and simple configuration , Embedded in the application can be released at the same time API Interface document interface , No need to deploy standalone Services .

Next , We go through Spring Boot To integrate Swagger Online API Functions of documents , This article USES SpringFox Swagger2, edition 2.9.2( The latest version is 3.0.0, But in my test is swagger-ui.html The page never came out , stay https://mvnrepository.com/ You can see ,2.9.2 Is currently the most used version )

Two 、 establish Spring Boot engineering

The development tool I use is IDEA, adopt IDEA Quickly create one Spring Boot engineering , Creation time , Just check web Dependent options become , It doesn't matter if you don't check it , There is pom.xml The configuration in is the same . Note that when creating a project , Project names should be in lowercase .

add to Swagger The two dependencies of

<!-- swagger -->

3、 ... and 、 Add configuration class

Add one swagger Configuration class , Build new under the project config Package and add a SwaggerConfig Configuration class SwaggerConfig.java.

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;
public class SwaggerConfig {
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title(" On-line API file ")
.description("This is a restful api document of demo")

The above has been completed Swagger Configuration of , Isn't it simple and easy .

Four 、 Create a test Controller To verify the configuration

Add a controller , Build new under the project controller Package and add a HelloController controller HelloController.java.

package com.louis.springboot.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
/* Class annotation */
public class HelloController {
/* Method notes */
@ApiOperation(value = "desc of method", notes = "")
public Object hello( @ApiParam(value = "desc of param" , required=true ) @RequestParam String name) {
return "Hello " + name + "!";

5、 ... and 、 Compile run test

Start the project ,  Open the browser , visit :http://localhost:8080/swagger-ui.html, Get into swagger Interface document interface .

Finish work , Manage like this API The interface is very convenient , And it is updated in real time with the code , Don't bother to write interface documents .

appendix :Swagger Commonly used annotations

API Use location
@Api be used for controller Class , Indicates that this class is swagger Resources for
@ApiOperation Use in controller On the way , It means a http Requested operation
@ApiParam Method
@ApiResponses Use in controller On the way
@ApiResponse Use in @ApiResponses inside
@ApiImplicitParams Use in controller On the way
@ApiImplicitParam Use in @ApiImplicitParams In the way of
@ApiModel For return object classes

WEB There are many strange problems in project development , You want to know how to quickly master Spring Boot, See video :

51CTO:Spring Boot+Bootstrap Development is small and complete web project
Tencent classroom :Spring Boot+Bootstrap Development is small and complete web project
CSDN college :Spring Boot+Bootstrap Development is small and complete web project
Netease cloud classroom :Spring Boot+Bootstrap Development is small and complete web project

本文为[Jiang Chengjun]所创,转载请带上原文链接,感谢

  1. 图解 Kafka 线程模型及其设计缺陷
  2. Add data files for Oracle tablespaces or temporary tablespaces
  3. Intellij IDEA神器居然还有这些小技巧,mysql集群搭建视频
  4. IntelliJ IDEA(2019)之Web项目创建,掌门一对一java面试题
  5. InnoDB(2,如何访问Redis中的海量数据
  6. InheritableThreadLocal使用详解,java多线程面试题及答案整理
  7. How does Oracle modify the data type of a column
  8. Oracle 12C management control file official document translation instructions
  9. Oracle 10g in Oracle Linux 5.4 32bit RAC installation manual (Yimo Xiyang)
  10. Oracle 12C in Oracle Linux 6.5 64bit installation manual
  11. 一天十道Java面试题----第一天(面向对象-------》ArrayList和LinkedList)
  12. Schéma du modèle de fil Kafka et de ses défauts de conception
  13. Starting and shutting down Oracle RAC database cluster
  14. CRS_ Oracle CRS stack is already configured and will be running under init(1M)
  15. Common skills of Oracle stored procedure
  16. Check the number of CPUs, core books and threads of the Linux system
  17. jQuery-实例方法
  18. Oracle de duplicated data
  19. jQuery-dom和jQuery,入口函数(基本知识)
  20. Oracle creates unique constraints on columns that already have duplicate data
  21. JavaScript-拷贝
  22. JavaScript-this指向问题
  23.  There is ^ [[a garbled code problem in the up and down keys in Oracle sqlplus
  24. JavaScript-封装与继承(两种)
  25. JavaScript-包装类型
  26. JavaScript-传值(引用类型,基本类型)
  27. JavaScript-面向对象(构造函数,实例成员,静态成员)
  28. JavaScript-解构赋值
  29. JavaScript-箭头函数
  30. JavaScript-参数
  31. JavaScript-预解析(变量提升)
  32. JavaScript-闭包closure
  33. JavaScript-声明变量的关键字
  34. JavaScript - mot - clé pour déclarer une variable
  35. Fermeture de fermeture JavaScript
  36. JavaScript Pre - parse (promotion des variables)
  37. Paramètres JavaScript
  38. Fonction de flèche JavaScript
  39. JavaScript - déconstruction assignations
  40. Common annotations in springboot
  41. Building CentOS 7.6 with Linux
  42. JavaScript - orienté objet (constructeur, membre d'instance, membre statique)
  43. JavaScript value Transfer (reference type, Basic type)
  44. JavaScript - type d'emballage
  45. linux deepin/ubuntu安装flameshot火焰截图
  46. JavaScript - encapsulation et héritage (deux)
  47. JavaScript JS method for writing 99 multiplication table
  48. 從零開始學java - 第二十五天
  49. Apprendre Java à partir de zéro - jour 25
  50. Les voitures d'hiver, les voitures électriques et les voitures à essence ne sont pas les mêmes?
  51. JavaScript - ceci pointe vers le problème
  52. Copie JavaScript
  53. Spring boot quickly integrates swagger
  54. linux deepin/ubuntu安裝flameshot火焰截圖
  55. Capture d'écran de flamme de l'installateur de flamme Linux deepin / Ubuntu
  56. Jquery DOM et jquery, fonctions d'entrée (bases)
  57. Méthode d'instance jquery
  58. Méthode et démonstration de code dans l'interface de liste en Java
  59. Démarrage du Zookeeper
  60. Java oom Cognition