This is the fourth day of my participation in the August More text Challenge. For details, see: August More Text Challenge
Introduction of Swagger
Swagger Swagger is a framework for easy API development that has the following advantages:
- Automatically generate online documentation, back-end developer changes can be immediately reflected in the online documentation, and do not have to write separate interface documentation, reducing the development burden
- Supported by
Swagger
Page direct debugging interface, convenient front-end developers for testing
Configuration Swagger
Swagger is currently available in two major versions, 2.x and 3.x, in slightly different configurations.
- Add the dependent
Swagger is a technology that follows the OpenAPI specification, and Springfox is an implementation of that technology, so search springfox instead of Swagger.
- for
Swagger 2.x
That need to be inpom.xml
To add two configurations:
<! -- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> < artifactId > springfox - swagger2 < / artifactId > < version > 2.9.2 < / version > < / dependency > <! -- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> < artifactId > springfox swagger - UI < / artifactId > < version > 2.9.2 < / version > < / dependency >Copy the code
- for
Swagger 3.x
, simplifies the configuration items needed only inpom.xml
To add a configuration:
<! -- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter --> <dependency> < the groupId > IO. Springfox < / groupId > < artifactId > springfox - the boot - starter < / artifactId > < version > 3.0.0 < / version > < / dependency >Copy the code
- Start for a project
Swagger
- for
Swagger 2.x
, the use of@EnableSwagger2
Annotations to openSwagger
Function.
@EnableSwagger2
@SpringBootApplication
public class SwaggerApplication {
...
}
Copy the code
- for
Swagger 3.x
, the use of@EnableOpenApi
Annotations to openSwagger
Function.
@EnableOpenApi
@SpringBootApplication
public class SwaggerApplication {
...
}
Copy the code
- create
SwaggerConfig
The configuration class
- for
Swagger 2.x
To instantiateDocket
We need to pass it inDocumentationType.SWAGGER_2
. - for
Swagger 3.x
To instantiateDocket
We need to pass it inDocumentationType.OAS_30
.
Here is a configuration template:
import org.springframework.beans.factory.annotation.Value; 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.*; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration public class SwaggerConfig { @Value("${spring.profiles.active:NA}") private String active; @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) // OAS_30 Swagger.apiinfo (apiInfo()).host("http://www.example.com") // Base URL only in the development environment .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo ApiInfo () {return new ApiInfoBuilder().title("API document ").description(" this is the description ").contact(new) Contact (" zhang ", null, null)) version (" 1.0 "). The build (); }}Copy the code
- access
Swagger
The front page
- for
Swagger 2.x
To accesshttp://localhost:8080/swagger-ui.html - for
Swagger 3.x
To accesshttp://localhost:8080/swagger-ui/
Controller related notes
@Api
: marks a class asSwagger
Resources, generally put inController
Class above, throughtags
Specify the description, such as@api (tags=" User management ")
.@ApiOperation
: This note is placed inController
Method above, describes what the method does.@ApiParam
: This note is placed inController
Before the parameter of a method, you can describe what the parameter does, such as@apiparam (" username ") String username
. You can usevalue
To specify the description, passrequired = true
Specifies that this parameter must be passed.
package com.example.swagger.controller; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @api (tags = "Hello controller ") @RestController public Class HelloController {@apiOperation (" Welcome message ") @getMapping ("/ Hello ") Public String hello(@apiparam (" name ") String name) {return "hello, "+ name; }}Copy the code
Entity related annotations
@ApiModel
: is usually placed on top of an entity class. Can be achieved byvalue
Specifies an alias. If not specified, the class name defaults. You can also go throughdescription
Specify detailed description. Such as@ ApiModel (" user ")
Will showThe user
Rather thanUser
.
! [] (cdn.jsdelivr.net/gh/jpch89/P… Use Swagger 00.png in Spring Boot)
If you want to specify only the description without changing the original class name display, you can write @apiModel (Description = “user “).
@ApiModelProperty
: is usually placed on the member variable of the entity classvalue
Specify the description,example
Specify sample data,required = true
Specifies that this parameter is required,hidden = true
Used to hide the field, not inAPI
Displayed in the document.
package com.example.swagger.pojo; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @data @apiModel (description = "User ") public class User {@apiModelProperty (" User ") private String username; @data @apiModel (description =" User ") public class User {@apiModelProperty (" User ") private String username; @apiModelProperty (" password ") private String password; @apiModelProperty (value = "age ", example = "18", required = true) private int age; @ApiModelProperty(hidden = true) private double money; }Copy the code
2021.08.04