Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Swagger in SpringBoot

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains how to use Swagger in SpringBoot. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to use Swagger in SpringBoot"!

Integrated Swagger

Dependency:

com.battcn swagger-spring-boot-starter 2.1.5-RELEASE

Swagger here should also be found, not official, this is a third-party integration, configuration is simpler.

configuration details

Detailed configuration:

spring: swagger: enabled: true title: Title description: descriptive information version: System version number contact: name: Maintainer information base-package: basic package for swagger scan, default: full scan (optional here in case of grouping) #Global parameters, such as authentication information like Token, can be configured globally global-operation-parameters: - description: "Token information, required" 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: Starry Sky Cabin-Articles Microservices Interface description: Article microservice-related interfaces, including article, module, knowledge point management, etc. version: 1.0.0 - SNAPSHOT contact: name: cv devil email: 1919301983@qq.com host: localhost enabled: true security: filter-plugin: true #Configure account password username: root password: root

Configuration interceptor, followed by interceptor configuration, if readers need to use in their own projects, please modify the original interceptor configuration, ignore the following path, so as not to be intercepted and unable to access. "swagger-ui.html", "static/css/", "static/js/", "swagger-resources", "/** /error", "v2/api-docs"

test uses

Run the project, visit IP+ port number/swagger-ui.html, for example in browser: http://127.0.0.1:13001/swagger-ui.html

Effects after login:

Review-frequently used notes

For swagger familiar friends, please ignore the "common comment paragraph"

`@Api`: Used on the Controller class to describe the role of the class 1. `value`="Description information" 2. `description`="Describe in detail what this class does"

@ApiOperation: Used on the Controller request method to describe what the method does.

@ApiModel: Used on the request parameter is an object to describe the role of the object class

//use @ApiModel@ApiModel(value="CategoryREQ object", description="CategoryQuery condition")public class CategoryREQ extends BaseRequest {} on object class

@ApiModelProperty: Used when the request parameter is an object property, describing the role of the object property.

value: Description of the attribute

hidden: whether it is a query condition attribute, false:(default) displayed in api documents as query condition;true hidden, not a condition attribute

//Request method argument is CategoryREQ object public Result search(@RequestBody CategoryREQ req) {}@ApiModel(value="CategoryREQ object", description="CategoryQuery condition")public class CategoryREQ extends BaseRequest { @ApiModelProperty(value = "category name") private String name; @ApiModelProperty(value="Status (1: Normal, 0: Disabled)") private Integer status;}

@ApiResponses: Used on request methods to represent a set of responses

@ApiResponse: Used in @ApiResponses, generally used to express an error response message, comment parameters:

code: number, such as 400message: message, such as "parameter filling error"response: class throwing exception

@ApiIgnore: Ignore this API with this annotation

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

@ApiImplicitParam: can be used alone or in @ApiImplicitParams to add a description to a request parameter of a method.

name: parameter name

value: describes the function of the parameter

dataType: Parameter type, Parameter type, Default String, Other valuesdataType ="Integer"

defaultValue: parameter default value

required: whether the parameter must be passed (true/false)

paramTpye: Specify where parameters are placed (header/query/path/body/form)

header: submit @RequestHeader in request headers

query: Complete automatic mapping assignment directly with parameters @RequestParam

path: submit data as a path variable @PathVariable

body: submitted as a stream only POST is supported (rarely used)

form: submitted as a form only POST is supported (rarely used)

Reference:

//request method has multiple request parameters size, current@ApiImplicitParams({ @ApiImplicitParam(name="current", value="page code", required=true, paramType="path",dataType="int"), @ApiImplicitParam (name="size", value="records per page", required=true, paramType="path",dataType="int")})@ApiOperation ("Query Category List Interface by Category Name and Status")@PostMapping ("/search/{current}/{size}")Result search (@RequestBody CategoryREQ req, @PathVariable int current, @PathVariable int size); At this point, I believe that everyone has a deeper understanding of "how to use Swagger in SpringBoot", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report