Our task is introduced

This blog is our Task: a complete list management system tutorial document on Github. It is a front and back separated list management tool developed by SpringBoot+Vue, which imitatestick lists. At present, it has been deployed on Ali Cloud ECS, which can be previewed online and used at will (with detailed tutorial). If you are interested, welcome to give a star!

Aliyun preview address

Integration steps

Maven rely on

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>  </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <! <dependency> <groupId> IO. Springfox </groupId> <artifactId> Springfox-Swagger2 </artifactId> < version > 2.7.0 < / version > < / dependency > < the dependency > < groupId >. IO springfox < / groupId > < artifactId > springfox swagger - UI < / artifactId > < version > 2.7.0 < / version > < / dependency > < / dependencies >Copy the code

The User entity

Create the User class for subsequent verification

@APIModelProperty for methods, fields; Represents a description or data manipulation change to a Model property.

Public class User{@APIModelProperty (value = "User ID ") Integer ID; @APIModelProperty (value = "username ") String username; @APIModelProperty (value = "user password ") String password; public User(String username, String password) { this.username = username; this.password = password; } @Override public String toString() { return "User{" + "username='" + username + '\'' + ", password='" + password + '\'' + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}Copy the code

Swagger – UI configuration class

You only need to modify the basePackage (” com. Example. Demo. Controller “), changed the to own the package name of the controller layer is ok, Swagger – the UI will scan the package under all of the controller, It is displayed on the front screen.

@Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket createRestApi(){ return new Docket (DocumentationType SWAGGER_2). ApiInfo (apiInfo ()). The select () / / for the current controller to generate the API documentation package .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo ApiInfo () {return new ApiInfoBuilder().title(" config ").description(" swagger-ui-demo ") Version (" 1.0 "). The build (); }}Copy the code

UserController

@restController@api (value = "user controller ", Tags = {" User operation "}) public class UserController {@getMapping ("/hello") @APIOperation (" hello") public String Hello (){return "Hello, I'm Xiaona "; } @postmapping ("/login") @apiOperation (" login") public String login(@requestBody @apiparam (" User object ") User User){return "hello" + user.toString(); }}Copy the code

Explanatory 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: Describes a request parameter. You can configure the Chinese meaning of the parameter and set the default value for the parameter
  • @APIIMPLICITParams: Describes a list of request parameters annotated by multiple @APIIMPLicitParam parameters

Form a complete set of code

After writing so much, I’m still worried that we can’t solve the problem, so I put the supporting Demo on Github. If you have any questions, you can download and run it.

Swagger-UI-Demo