Swagger can help us solve most of our problems (automatically generate API documentation).

He’s going to generate an HTML page for us, something like this.

Ok, start the text, if you feel the need to read down.

1. Add dependencies

< the dependency > < groupId > IO. Springfox < / groupId > < artifactId > springfox - swagger2 < / artifactId > < version > 2.6.1 < / version > </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> The < version > 2.6.1 < / version > < / dependency >Copy the code

2. Modify the boot option

Add annotations

@enablesWagger2 // EnableSwagger document generationCopy the code

3. Add comments to Controller or field

3.1 Add comments to the Controller method.
@ ApiOperation (value = "condition query users") @ GetMapping ("/user ") @ JsonView (user. UserSimpleView. Class) public List query(UserQueryCondition condition, @PageableDefault(page = 2,size = 7,sort = "username,asc")Pageable pageable){ System.out.println(ReflectionToStringBuilder.toString(condition, ToStringStyle.DEFAULT_STYLE)); List<User> users = new ArrayList<>(); users.add(new User()); users.add(new User()); users.add(new User()); return users; }Copy the code

And then visit http://127.0.0.1:8080/swagger-ui.html

3.2 Add comments to fields in a method

Method one:

@requestMapping ("/user/{id:\\d+}") @apiImplICITParam (name = "id",value = "user id") public user getInfo(@pathVariable String id){ User user = new User(); user.setUsername("FantJ"); return user; }Copy the code

Method 2:

@requestMapping ("/user/{id:\\d+}") public user getInfo(@apiParam (" user ID ") @pathVariable String ID){user user = new User(); user.setUsername("FantJ"); return user; }Copy the code

Method one is to annotate the method, method two is to annotate the parameter bits.

3.3 Adding comments to attributes of an entity class
@apiModelProperty (" username ") private String username;Copy the code

### Summary of all final notes

  • @Api: Decorates the entire class to describe what Controller does
  • ApiOperation: Describes a method, or interface, of a class
  • @APIParAM: Single parameter description
  • @APIModel: Accept arguments with objects
  • ApiProperty: A field describing an object when receiving a parameter with the object
  • @apiResponse: HTTP responds to one of the descriptions
  • @apiresponses: Overall description of HTTP responses
  • @apiIgnore: Ignore the API with this annotation
  • @apiError: Information returned when an error occurs
  • ApiImplicitParam: A request parameter
  • ApiImplicitParams: Multiple request parameters