Detailed notes for swagger

​
|    **Scope of action**    |      **API**       |                       **API Common parameters**                        |         **Action position**         |
| :----------------: | :----------------: | :----------------------------------------------------------: | :--------------------------: |
|     Protocol set description     |        @Api        |              @Api(tags = {"tag1","tag2","..."})              |         controller class         |
|      Protocol description      |   @ApiOperation    |       @ApiOperation(value = "Function description",notes = "remarks")       |      controller Class method      |
| Describes the meaning of the returned object |     @ApiModel      |         @ApiModel(value="Class name",description="Class description")         |          Return object class          |
|      Object properties      | @ApiModelProperty  | @ApiModelProperty(value = "Class attribute description",required = *true*,example = "Attribute example",notes = "remarks") |      Field of access parameter object      |
|    Non object parameter set    | @ApiImplicitParams | @ApiImplicitParams({@ApiImplicitParam(),@ApiImplicitParam(),...}) |       controller Method of       |
|   Non object parameter description   | @ApiImplicitParam  | @ApiImplicitParam(name = "Parameter name",value = "Parameter description",required = *true*,paramType = "Interface parameter type",dataType = "Parameter data type") | @ApiImplicitParams Used in the method of |
|     Response collection     |   @ApiResponses    |    @ApiResponses({     @ApiResponse(),@ApiResponse(),..})    |       controller Method of       |
|      Response      |    @ApiResponse    |       @ApiResponse(code = 10001, message = "Return information")       |      @ApiResponses In use       |
|      Ignore comments      |     @ApiIgnore     |                          @ApiIgnore                          |      Class, method, method parameter      |

API usage details

@Api

Function: used to specify the description text of the interface
Modification scope: acts on a class
@Api(tags = "TestController test")
@RestController
public class TestController {
    ....
}

@ApiOperation

Function: used to describe specific methods in the interface
Scope of modification: act on methods
@ApiOperation(value = "Overall interface description",notes = "<span style='color:red;'>Detailed description:</span>&nbsp;Method details")
@GetMapping("/")
public String login(String... index) {
    return "Hello login ~";
}
value: used to describe the overall interface
notes: used to describe the interface in detail

@ApiImplicitParams

Function: used to describe parameters in the interface
Scope of modification: act on methods
Parameters:@ ApiImplicitParam array

@ApiImplicitParam

Function: modify the parameters in the interface method
Modification scope: action method
Parameters:
  • name: Method parameters name
  • value: description of method parameters
  • dataType: method parameter data type
  • defaultValue  : Method parameter default value (for testers to test)
  • paramType :
    • Default query: corresponding method 1
    • path: corresponding mode 2
    • body: corresponding mode 3

Method 1: url? Parameters after id = 1 & user ='qlh '

@ApiOperation(value = "Overall interface description", notes = "<span style='color:red;'>Detailed description:</span>&nbsp;Method details")
@ApiImplicitParams({
        @ApiImplicitParam(name = "username", value = "user name", dataType = "String", defaultValue = "qlh"),
        @ApiImplicitParam(name = "password", value = "password", dataType = "String", defaultValue = "123")
})
@PostMapping("/")
public String login(String username, String password) {
    return "Hello login ~";
}

Method 2: pass parameters after url/1/2 path and obtain parameters in the path

@ApiOperation(value = "Overall interface description", notes = "<span style='color:red;'>Detailed description:</span>&nbsp;Method details")
@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", dataType = "String", defaultValue = "qlh",paramType = "path"),
        @ApiImplicitParam(name = "name", value = "full name", dataType = "String", defaultValue = "123",paramType = "path")
})
@PostMapping("/index/{id}/{name}")
public String index(@PathVariable("id") String id, @PathVariable("name") String name) {
    return "Hello World ~";
}

Method 3: pass parameters in the body

@ApiOperation(value = "Overall interface description", notes = "<span style='color:red;'>Detailed description:</span>&nbsp;Method details")
@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "id", dataType = "String", defaultValue = "xxx", paramType = "body"),
        @ApiImplicitParam(name = "name", value = "full name", dataType = "String", defaultValue = "123", paramType = "body")
})
@PostMapping("/index")
public String index(@RequestBody Map<String, Object> map) {
    return "Hello World ~";
}

@ApiResponses

Function: used for the response result of the interface
Modification scope: works on interface methods
Parameters:@ ApiResponse array
@ApiResponses({
        @ApiResponse(),
        @ApiResponse(),
        ...
})

@ApiResponse

Function: set the response code and response content in ApiResponses
Scope of modification: on the interface method
Parameters:
  • Code: response status code
  • message: the response content corresponding to the response status code
@ApiResponse(code = 10001, message = "Signature error"),
@ApiResponse(code = 10002, message = "sql error"),
@ApiResponse(code = 10003, message = "Service slack,Please try again later"),

@ApiIgnore

Function: ignore classes, methods and parameters. (omitted means: in swagger-ui.html (not shown in)
Modification scope: acts on classes, methods and parameters
@ApiIgnore

Entity class swagger annotation in

@ApiModel

Function: used to describe entity classes
Modification scope: acts on a class
@ApiModel(value="Class name",description = "Entity class description")

@ApiModelProperty

Function: used to describe the attributes in the entity class
Modification scope: acts on attributes in a class
@ApiModelProperty(value = "Class attribute description",required = true,example = "Attribute example",notes = "remarks")

Conclusion

thus The spring boot integration with swagger 2 is over. I believe that after reading these two articles, you will be able to use swagger skillfully in java code.
Because at present Front and rear end separation is more popular, so write a good one swagger interface documentation is very necessary, which will reduce the communication between back-end developers and front-end personnel because some interfaces are not clearly expressed, which greatly improves the development efficiency.

 

Keywords: Spring Boot swagger2

Added by mbowling on Thu, 09 Dec 2021 07:12:11 +0200