How to use Swagger in SpringBoot
Integrating Swagger
Dependencies:
<!-- Swagger --> <dependency> <groupId>com.battcn</groupId> <artifactId>swagger-spring-boot-starter</artifactId> <version>2.1.5-RELEASE</version> </dependency>
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
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
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
The effect after logging in:
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> { }
@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
##@ApiIgnore: message, such as "parameter filling error"
response: The class that threw the exception
: 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.
- :Parameter name
value :Describe the function of the parameter
dataType : parameter type, parameter type, default String, other values dataType="Integer"
: Parameter default value##defaultValue
: Whether the parameter must be passed (true/false)required
: Specify where the parameters are placed (header/query/path/body/form)paramTpye
: The parameters are submitted in the request headers @RequestHeaderheader
: 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);
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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 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

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

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

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

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

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
