Table of Contents
Integrating Swagger
Configuration details
Test using
Home Java javaTutorial How to use Swagger in SpringBoot

How to use Swagger in SpringBoot

May 17, 2023 pm 08:16 PM
springboot swagger

Integrating Swagger

Dependencies:

<!-- Swagger -->
<dependency>
    <groupId>com.battcn</groupId>
    <artifactId>swagger-spring-boot-starter</artifactId>
    <version>2.1.5-RELEASE</version>
</dependency>
Copy after login

You may have noticed that the Swagger I have here is not official. This is integrated by a third party, and the configuration is simpler.

Configuration details

Detailed configuration:

spring:
  swagger:
    enabled: true
    title: 标题
    description: 描述信息
    version: 系统版本号
    contact:
      name: 维护者信息
    base-package: swagger扫描的基础包,默认:全扫描(分组情况下此处可不配置)
    #全局参数,比如Token之类的验证信息可以全局话配置
    global-operation-parameters:
    -   description: "Token信息,必填项"
        modelRef: "string"
        name: "Authorization"
        parameter-type: "header"
        required: true
    groups:
      basic-group:
        base-package: com.battcn.controller.basic
      system-group:
        base-package: com.battcn.controller.system
Copy after login

My configuration

spring:
  swagger:
    title: 星空小屋 - 文章微服务接口
    description: 文章微服务相关接口,包括文章、模块、知识点管理等
    version: 1.0.0 - SNAPSHOT
    contact:
      name: cv大魔王
      email: 1919301983@qq.com
    host: localhost
    enabled: true
    security:
      filter-plugin: true # 配置账号密码
      username: root
      password: root
Copy after login

Configure the interceptor, there is interceptor configuration later, if any readers need to configure it themselves For project use, please modify the original interceptor configuration and ignore the following paths to avoid being intercepted and resulting in inaccessibility. "swagger-ui.html", "static/css/", "static/js/", "swagger-resources", "/**/error", "v2/api-docs"

Test using

to run the project and access IP port number/swagger-ui.html, for example, access in the browser: http://127.0.0.1:13001/swagger -ui.html

How to use Swagger in SpringBoot

The effect after logging in:

How to use Swagger in SpringBoot

##Review - Commonly used annotations

For those who are familiar with swagger, please ignore the "common annotation paragraph"

`@Api`:用在 Controller 类上,描述该类的作用
  1. `value`="描述信息"
  2. `description`="详细描述该类的作用"
Copy after login

@ApiOperation: Used on the Controller request method to describe the function of the method.

@ApiModel: Used when the request parameter is an object, describing the role of the object class

// 在对象类上使用@ApiModel
@ApiModel(value="CategoryREQ对象", description="类别查询条件")
public class CategoryREQ extends BaseRequest<Category> {
}
Copy after login

@ApiModelProperty: Used when the request parameter is On the properties of the object, describe the role of the object's properties.

  • value: Description of the attribute

  • ##hidden

    : Whether it is a query condition attribute, false : (default value) is displayed in the api document as a query condition; true is hidden, not a conditional attribute

    // 请求方法参数是 CategoryREQ 对象
    public Result search(@RequestBody CategoryREQ req) {}
    
    @ApiModel(value="CategoryREQ对象", description="类别查询条件")
    public class CategoryREQ extends BaseRequest<Category> {
        
        @ApiModelProperty(value = "分类名称")
        private String name;
    
        @ApiModelProperty(value="状态(1:正常,0:禁用)")
        private Integer status;
    }
    Copy after login
  • @ApiResponses

    : Used in In the request method, it is used to represent a set of responses

  • ##@ApiResponse
  • : used in

    @ApiResponses, generally used to express an error Response information, annotation parameters:

  • code
  • : number, such as 400

    message: message, such as "parameter filling error" response: The class that threw the exception

    ##@ApiIgnore
  • : Use this annotation to ignore this API

@ApiImplicitParams: Used on request methods to add descriptions to multiple request parameters

@ApiImplicitParam: Can be used alone or in @ApiImplicitParams to add descriptions to one request parameter of the method.

name
    :Parameter name
  1. value
  2. :Describe the function of the parameter
  3. dataType
  4. : parameter type, parameter type, default String, other values ​​dataType="Integer"
  5. ##defaultValue

    : Parameter default value
  6. required

    : Whether the parameter must be passed (true/false)
  7. paramTpye

    : Specify where the parameters are placed (header/query/path/body/form)
  8. header

    : The parameters are submitted in the request headers @RequestHeader
query

: Complete automatic mapping and assignment directly with parameters @RequestParampath
: Submit data in the form of path variables @PathVariablebody
: Submitting in the form of a stream only supports POST (not commonly used) form
: Submitting in the form of a form only supports POST (not commonly used) Reference:

// 请求方法有多个请求参数 size, current
@ApiImplicitParams({
    @ApiImplicitParam(name="current", value="页码", required=true, paramType="path",dataType="int"),
    @ApiImplicitParam(name="size", value="每页记录数", required=true, paramType="path",dataType="int")
})
@ApiOperation("根据分类名称与状态查询分类列表接口")
@PostMapping("/search/{current}/{size}")
Result search(@RequestBody CategoryREQ req, @PathVariable int current, @PathVariable int size);
Copy after login

The above is the detailed content of How to use Swagger in SpringBoot. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How Springboot integrates Jasypt to implement configuration file encryption How Springboot integrates Jasypt to implement configuration file encryption Jun 01, 2023 am 08:55 AM

Introduction to Jasypt Jasypt is a java library that allows a developer to add basic encryption functionality to his/her project with minimal effort and does not require a deep understanding of how encryption works. High security for one-way and two-way encryption. , standards-based encryption technology. Encrypt passwords, text, numbers, binaries... Suitable for integration into Spring-based applications, open API, for use with any JCE provider... Add the following dependency: com.github.ulisesbocchiojasypt-spring-boot-starter2. 1.1Jasypt benefits protect our system security. Even if the code is leaked, the data source can be guaranteed.

How to generate API documentation using Swagger in PHP How to generate API documentation using Swagger in PHP Jun 17, 2023 am 10:40 AM

With the continuous development of web applications, API has become one of the standards for modern web application development. However, as the number and complexity of APIs increases, maintaining and documenting them becomes increasingly complex. To solve this problem, Swagger came into being. It is a tool for generating API documentation, making it easier for developers to maintain and document APIs, while also providing visual documentation and various other features. In this article, we will discuss how to use Swagger in PHP to generate a

Laravel development: How to use Laravel Swagger to generate API documentation? Laravel development: How to use Laravel Swagger to generate API documentation? Jun 13, 2023 am 09:35 AM

Laravel development: How to use LaravelSwagger to generate API documentation? When developing web applications, dealing with API documentation is often a tedious but essential task. Use Swagger to automatically generate and visualize API documentation. In Laravel development, we can use the LaravelSwagger extension package to easily generate SwaggerAPI documentation. This article will guide you how to use L

How to use Redis to implement distributed locks in SpringBoot How to use Redis to implement distributed locks in SpringBoot Jun 03, 2023 am 08:16 AM

1. Redis implements distributed lock principle and why distributed locks are needed. Before talking about distributed locks, it is necessary to explain why distributed locks are needed. The opposite of distributed locks is stand-alone locks. When we write multi-threaded programs, we avoid data problems caused by operating a shared variable at the same time. We usually use a lock to mutually exclude the shared variables to ensure the correctness of the shared variables. Its scope of use is in the same process. If there are multiple processes that need to operate a shared resource at the same time, how can they be mutually exclusive? Today's business applications are usually microservice architecture, which also means that one application will deploy multiple processes. If multiple processes need to modify the same row of records in MySQL, in order to avoid dirty data caused by out-of-order operations, distribution needs to be introduced at this time. The style is locked. Want to achieve points

How to solve the problem that springboot cannot access the file after reading it into a jar package How to solve the problem that springboot cannot access the file after reading it into a jar package Jun 03, 2023 pm 04:38 PM

Springboot reads the file, but cannot access the latest development after packaging it into a jar package. There is a situation where springboot cannot read the file after packaging it into a jar package. The reason is that after packaging, the virtual path of the file is invalid and can only be accessed through the stream. Read. The file is under resources publicvoidtest(){Listnames=newArrayList();InputStreamReaderread=null;try{ClassPathResourceresource=newClassPathResource("name.txt");Input

How to implement Springboot+Mybatis-plus without using SQL statements to add multiple tables How to implement Springboot+Mybatis-plus without using SQL statements to add multiple tables Jun 02, 2023 am 11:07 AM

When Springboot+Mybatis-plus does not use SQL statements to perform multi-table adding operations, the problems I encountered are decomposed by simulating thinking in the test environment: Create a BrandDTO object with parameters to simulate passing parameters to the background. We all know that it is extremely difficult to perform multi-table operations in Mybatis-plus. If you do not use tools such as Mybatis-plus-join, you can only configure the corresponding Mapper.xml file and configure The smelly and long ResultMap, and then write the corresponding sql statement. Although this method seems cumbersome, it is highly flexible and allows us to

Comparison and difference analysis between SpringBoot and SpringMVC Comparison and difference analysis between SpringBoot and SpringMVC Dec 29, 2023 am 11:02 AM

SpringBoot and SpringMVC are both commonly used frameworks in Java development, but there are some obvious differences between them. This article will explore the features and uses of these two frameworks and compare their differences. First, let's learn about SpringBoot. SpringBoot was developed by the Pivotal team to simplify the creation and deployment of applications based on the Spring framework. It provides a fast, lightweight way to build stand-alone, executable

How SpringBoot customizes Redis to implement cache serialization How SpringBoot customizes Redis to implement cache serialization Jun 03, 2023 am 11:32 AM

1. Customize RedisTemplate1.1, RedisAPI default serialization mechanism. The API-based Redis cache implementation uses the RedisTemplate template for data caching operations. Here, open the RedisTemplate class and view the source code information of the class. publicclassRedisTemplateextendsRedisAccessorimplementsRedisOperations, BeanClassLoaderAware{//Declare key, Various serialization methods of value, the initial value is empty @NullableprivateRedisSe

See all articles