Swagger solves this problem gracefully. It is a powerful API framework, and its integration is very simple, not only provides online document lookup, but also provides online document testing. Swagger is a canonical and complete framework for generating, describing, invoking, and visualizing RESTful Web services. The overall goal is to have clients and file systems update at the same rate as servers. File methods, parameters, and models are tightly integrated into server-side code, allowing the API to always be in sync. Swagger makes deployment management and using powerful apis easier than ever.

Maven integrates Swagger

Recent versions can go to the central warehouse queries mvnrepository.com/artifact/co…

<! --> <dependency> <groupId> IO. Springfox </groupId> <artifactId> Springfox - Swagger2 </artifactId> < version > 2.8.0 < / version > < / dependency > < the dependency > < groupId >. IO springfox < / groupId > < artifactId > springfox swagger - UI < / artifactId > < version > 2.8.0 < / version > < / dependency > <! <dependency> <groupId>com.github. Xiaoymin </groupId> <artifactId>swagger-bootstrap-ui</artifactId> The < version > 1.9.6 < / version > < / dependency >Copy the code

2. Config configuration

package com.riskeys.heb.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @EnableSwagger2 @Configuration class Swagger2Config { @Bean public Docket createRestApi() { return new Docket (DocumentationType SWAGGER_2). GroupName (" Harbin project interface management ") / / specified grouping, corresponding (/ v2 / API docs? Group =). PathMapping ("") // Base address, Will eventually joining together the address of the Controller. ApiInfo (apiInfo ()). The select () / / for the current package path / /. Apis (RequestHandlerSelectors. Any ()) / / scanning path .apis(RequestHandlerSelectors.basePackage("com.riskeys.heb")) .paths(PathSelectors.any()) .build(); Private ApiInfo ApiInfo () {return new ApiInfoBuilder() // page title.title (" Haerbin project interface document ") // creator . Contact (new contact (" xiuxian. Wang ", ""," ")) / / version number, version (" 1.0 ") / / description. The description (" interface documentation "). The build (); }}Copy the code

3. Release static resources in web configuration

 @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
Copy the code

4. Use of annotations

1) Common annotation @API: Decorates the entire class and describes 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 parameter

@apiIMPLICITParams multiple parameters,

2) Code examples

@slf4j @restController @requestMapping ("/business") @API (value = "BusinessController",tags = "business related interface ")// For public class BusinessController {@postMapping (value = "/rescue/download") @apiOperation (" rescue ")// Used for method public void rescuePlan(@RequestBody @Valid RescuePlanDto dto, HttpServletRequest request, HttpServletResponse response) { log.info("rescue pdf request info is :{}",JSONObject.toJSONString(dto)); PdfReader reader; OutputStream os = null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfStamper stamper; } @data public class RescuePlanDto {@notBlank (message = "workorder ID cannot be empty ") @APIModelProperty (value =" workorder ID ", Required = True)// For field description and mandatory description private String workId; @notBlank (message = "the type cannot be empty ") @APIModelProperty (value =" the type ", Required = true) private String type; // Salvage type}Copy the code

5. Start the project access interface management platform

1, the beautification of the platform path http://{ip}:{port}/doc.html

2. Basic version http://{ip}:{port}/swagger-ui.html

6. Document output

Click copy the document and paste it into the editable Markdown file editor (Typora) to export the PDF in various formats