Spring-boot is currently the most popular Java Web Development scaffold, and more and more developers choose to build an enterprise-class RESTful API interface. These interfaces not only serve the traditional web-side (b/s), but also serve the mobile side. In the actual development process, these interfaces will also provide for the development of testing related to the White box test, then there is bound to be how to share in multi-person collaboration and timely Update API development interface documentation issues.
If you are already disgusted with the drawbacks of traditional wiki document sharing, try a Swagger2 approach that will make you have a different development experience.
Using the Swagger integration documentation has several advantages:
- Feature-rich: support a variety of annotations, automatically generate interface document interface, to support the interface testing API interface functions;
- Timely update: The development process spend a little time to write notes, you can update the API documentation in a timely manner, worry and effort;
- Simple integration: With the addition of POM dependencies and simple configuration, the API interface document interface can be published simultaneously in the application, without the need to deploy standalone services.
1. Add dependency
To add Maven dependencies, choose the 2.8.0 version here.
<!--Swagger --<Dependency> <GroupId>Io.springfox</GroupId> <Artifactid>Springfox-Swagger2</Artifactid> <Version>2.8.0</Version></Dependency><Dependency> <GroupId>Io.springfox</GroupId> <Artifactid>Springfox-Swagger-Ui</Artifactid> <Version>2.8.0</Version></Dependency>
2. Add a Configuration Class
Add the Swagger configuration class and add the Swaggerconfig configuration class under the Config package for the Kitty-boot project.
PackageCom.louis.kitty.boot.config;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;ImportSpringfox.documentation.builders.ApiInfoBuilder;Importspringfox.documentation.builders.PathSelectors;Importspringfox.documentation.builders.RequestHandlerSelectors;ImportSpringfox.documentation.service.ApiInfo;ImportSpringfox.documentation.spi.DocumentationType;ImportSpringfox.documentation.spring.web.plugins.Docket;ImportSpringfox.documentation.swagger2.annotations.EnableSwagger2, @Configuration @enableswagger2 Public classSwaggerconfig {@Bean PublicDocket Createrestapi () {return NewDocket (documentationtype.swagger_2). Apiinfo (Apiinfo ()). Select (). APIs (Requesthandlerse Lectors.any ()). Paths (Pathselectors.any ()). build (); } Privateapiinfo Apiinfo () {return NewApiinfobuilder (). Title ("Kitty API Doc"). Description ("This is a RESTful API document of Kitty."). Version ("1.0"). build (); }}
3. Start the test
Launch Kittyapplication, browser access: http://localhost:8088/swagger-ui.html#/
We see that Swagger has been integrated, select Sys-user-controller, click Try it out---execute, and the results return successfully.
4. Common annotations
Swagger generate documents through the annotation interface, including interface names, request methods, parameters, return information, etc.
@Api: Modifies the entire class for use on the Controller class
@ApiOperation: Describes an interface on the user controller method
@ApiParam: Single parameter description
@ApiModel: Object is used to receive parameters, that is, return objects
@ApiModelProperty: When an object receives a parameter, the field that describes the object
@ApiResponse: The description of the HTTP response, in Apiresonse
@ApiResponses: HTTP response for all descriptions, used in
@ApiIgnore: Ignore this API
@ApiError: The return information of the error occurred
@ApiImplicitParam: a request parameter
@ApiImplicitParam: Multiple Request parameters
For more instructions refer to the Swagger user manual
5. Use Cases
PackageCom.louis.kitty.admin.controller;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.GetMapping;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestParam;ImportOrg.springframework.web.bind.annotation.RestController;ImportCom.louis.kitty.admin.sevice.SysUserService;ImportIo.swagger.annotations.Api;ImportIo.swagger.annotations.ApiImplicitParam;Importio.swagger.annotations.ApiOperation; @Api (Value= "User Controller") @RestController @requestmapping ("User") Public classSysusercontroller {@AutowiredPrivateSysuserservice Sysuserservice; @ApiOperation (Value= "Get user Information", notes= "get user information based on user ID") @ApiImplicitParam (name= "UserId", value = "User id", required =true, DataType = "Long") @GetMapping (value= "/findbyuserid") PublicObject Findbyuserid (@RequestParam Long userId) {returnSysuserservice.findbyuserid (userId); } @GetMapping (Value= "/findall") PublicObject FindAll () {returnSysuserservice.findall (); }}
6. References
Official website: https://swagger.io/
User manual: Https://gumutianqi1.gitbooks.io/specification-doc/content/tools-doc/spring-boot-swagger2-guide.html
Online Tutorial: https://www.cnblogs.com/xiaohanghang/p/6018654.html
The rain recalls the light dust
Source: https://www.cnblogs.com/xifengxiaoma/
All rights reserved, welcome reprint, Reprint please indicate the original author and source.
Java Backend Management System (VI): Integrated Swagger API