The backend interface in the project is displayed in a simple front-end manner
Swagger is an open source tool for designing, building, documenting and using RESTful Web services. Swagger3 is the latest version of Swagger, which offers many new features and improvements.
The introduction method of Swagger in SpringBoot3 has changed, and most of the online versions are still SpringBoot2.
springboot version 3.2.4
1. Dependency introduction
Use maven to build a SpringBoot3 project, introduced in dependencies, and added in
<dependency> <groupId></groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>2.0.4</version> </dependency>
The new version can also be used, Springdoc-OpenAPI website link
2. Quick start
1. Configure in
# swagger-ui custom path springdoc: swagger-ui: path : /
2.or properties file, then configure
The code is as follows (example):
# swagger-ui custom path =/
3. Start the project to access swagger
Visit http://localhost:9090/swagger-ui/#/
9090 is changed to the port used by your project's backend. Be careful not to omit the following
3. Use annotation interface
Swagger configuration file
package ; import .; import .; import ; import ; @Configuration public class Swagger3Config { @Bean public OpenAPI springOpenAPI() { // Access path: http://localhost:9090/swagger-ui/ return new OpenAPI().info(new Info() .title("SpringDoc API") .description("SpringDoc Simple Application") .version("0.0.1")); } }
Swagger Annotation Migration
Swagger2 and Swagger3 use two completely different sets of annotations, so the code page that originally used Swagger2-related annotations needed to be completely migrated, and instead used Swagger3 annotations.
Swagger2 | Swagger3 |
---|---|
@Api | @Tag |
@ApiOperation | @Operation |
@ApiImplicitParams | @Parameters |
@ApiImplicitParam | @Parameter |
@ApiModel | @Schema |
@ApiModelProperty | @Schema |
@ApiResponses | @ApiResponses |
@ApiResponse | @ApiResponse |
@ApiIgnore | @Hidden or other annotations hidden = true attribute |
Five common uses
@Api
Swagger2 code
@Api(value = "User Interface", tags = "UserController")
Swagger3 code
@Tag(name = "UserController", description = "User Interface")
@ApiOperation
Swagger2 code
@ApiOperation(value = "Query user data")
Swagger3 code
@Operation(description = "Query user data")
@ApiImplicitParam
Swagger2 code
@ApiImplicitParams({ @ApiImplicitParam(name = "currentPage", value = "Current page number", dataTypeClass = , required = true), @ApiImplicitParam(name = "size", value = "Current page size", defaultValue = "10", dataTypeClass = ), @ApiImplicitParam(name = "queryUser", value = "User Query Conditions", dataTypeClass = ) })
Swagger3 code
@Parameters({ @Parameter(name = "currentPage", description = "Current page number", required = true), @Parameter(name = "size", description = "Current page size", example = "10"), @Parameter(name = "queryUser", description = "User Query Conditions") })
@ApiModel
Swagger2 code
@ApiModel(value = "User Information Entity Class")
Swagger3 code
@Schema(name = "User Information Entity Class")
@ApiModelProperty
Swagger2 code
@ApiModelProperty(value = "User Name")
Swagger3 code
@Schema(name = "User Name")
Example of usage
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import .; import .; import ; import ; import .*; import ; import .*; @RestController @RequestMapping("user") @CrossOrigin @Tag(name = "UserController", description = "User Interface") public class UserController { @Resource UserService userService; @Resource DepartmentService departmentService; /** * User registration * * @param registerUser * @return */ @Operation(description = "User Registration") @PostMapping("register") public R register(@RequestBody User registerUser) { if (userService .query() .eq(User.COL_CERTIFICATENO, ()) .count() > 0) { return ().code(0).message("The user already exists!").build(); } else { (registerUser); return ().code(200).message("Register successful! Please wait for the organization administrator to review...").build(); } } }
This is the end of this article about SpringBoot3 using Swagger3. For more related content about SpringBoot3 using Swagger3, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!