About the Swagger

Swagger is one of the most popular REST APIs document generation tools for several reasons:

  • MSwagger generates an interactive API console that developers can use to quickly learn and try out the API.
  • Swagger can generate client SDK code for implementation on a variety of different platforms.
  • Swagger files can be automatically generated from code comments on many different platforms.
  • Swagger has a strong community of tough contributors.
  • The Swagger document provides a way to describe your API with a specified JSON or YAML digest, including API information such as Names, Order, etc.

You can edit the Swagger file through a text editor, or you can generate it automatically from your code comments. Various tools can use Swagger files to generate interactive API documents.

Note: Generating interactive API documentation with Swagger files is the most streamlined, showing resources, parameters, requests, and responses. But it doesn’t give you any other details about how your API works. \

Brief introduction:

Swagger2 can display all the interfaces of a project in one UI interface, and indicate the purpose of the interface, the type of parameters required by the interface and whether the parameters are necessary. If the parameters are entered, it can directly test the functions of the interface like Postman, which will display the status code of the interface request and the data structure returned.

Advantages:

  1. Significantly reduce communication between the front and back ends
  2. Easy to find and test interface
  3. Improve team development efficiency
  4. It is convenient for newcomers to understand the project

Disadvantages:

The code is more intrusive

Note:

The online environment should close Swagger to avoid being attacked

See the previous article for swagger and Spring Boot integration

Here’s swagger

@Api()Used in the class; Tags - indicates that this class is Swagger's resource tags - indicates that value - also indicates that you can use tags instead but tags with multiple values will generate multiple lists@Api(value="User controller",tags={"User operation interface"})
@RestController
public class UserController {}Copy the code
@ApiOperation()Used for methods; An action representing an HTTP request Value used to describe a method notes used to indicate content Tags can be regrouped (as appropriate)@ApiParam()Used for method, parameter, field description; Name - Parameter name value - Parameter Description Required - Mandatory Specifies whether to add metadata to a parameter@Api(value="User controller",tags={"User operation interface"})
@RestController
public class UserController {
     @ApiOperation(value="Get user information",tags={"Get user information copy"},notes="Pay attention to problem spots.")
     @GetMapping("/getUserInfo")
     public User getUserInfo(@ApiParam(name="id",value="User id",required=true) Long id,@ApiParam(name="username",value="Username") String username) {
     // userService is negligible and is business logic
      User user = userService.getUserInfo();
      returnuser; }}Copy the code
@ApiModel() for classes; The entity class receives value - the object name - description - the description can be omitted@ApiModelProperty() for methods, fields; Value - Field Description name - Rewrite attribute name dataType - Rewrite attribute type Required - Mandatory or not - Example - Example - Hidden - Hidden@ApiModel(value="The user object",description="User object user")
public class User implements Serializable{
    private static final long serialVersionUID = 1L;
     @ApiModelProperty(value="Username",name="username",example="xingguo")
     private String username;
     @ApiModelProperty(value="State",name="state",required=true)
      private Integer state;
      private String password;
      private String nickName;
      private Integer isDeleted;
 
      @ApiModelProperty(value=Array "id",hidden=true)
      private String[] ids;
      private List<String> idList;
     Get/set / / ignore
}
Copy the code
@ApiIgnoreSwagger (); swagger (); swagger ()Copy the code
@ApiImplicitParam()Method to represent individual request parameters@ApiImplicitParams()For methods, including more than one@ApiImplicitParamName - Ming value - dataType - dataType paramType - parameter type example - provides an example@ApiOperation("Query test")
  @GetMapping("select")
  // @apiIMPLICITParam (name="name",value=" username ",dataType="String", paramType =" query")
  @ApiImplicitParams({
  @ApiImplicitParam(name="name",value="Username",dataType="string", paramType = "query",example="xingguo").@ApiImplicitParam(name="id",value="User id",dataType="long", paramType = "query")})
  public void select(){
}
Copy the code
Give a [look], is the biggest support for IT elder brotherCopy the code