This is the 11th day of my participation in the More text Challenge. For more details, see more text Challenge

preface

Swagger UI allows anyone, whether it’s your development team or your end consumer, to visualize and interact with API resources without any implementation logic. It is automatically generated according to your OpenAPI (formerly known as Swagger) specification, with visual documentation for easy back end implementation and client use. To put it simply, it is actually a normative and complete framework, which is mainly used for online automatic generation of interface documents and functional testing, and provides a graphical and operational interface for testing and front-end developers to facilitate functional testing of API interfaces. This article mainly introduces the specific process and steps of Integrating SpringBoot Swagger.

Code Procedure

1. Pom files import dependencies

< the dependency > < groupId > IO. Springfox < / groupId > < artifactId > springfox - swagger2 < / artifactId > < version > 2.9.2 < / version > </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> < version > 2.9.2 < / version > < / dependency > < the dependency > < groupId > com. Making. Caspar - Chen < / groupId > < artifactId > swagger - UI - layer < / artifactId > < version > 1.1.3 < / version > < / dependency >Copy the code

2. Declare the SwaggerConfig class

@configuration @enableswagger2 public class SwaggerConfig {public static final String VERSION = "1.0.0"; @Bean public Docket customImplementation(){ return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .build() .enable(true) .globalOperationParameters(getGlobalOperationParameters()) .apiInfo(apiInfo()); } private List<Parameter> getGlobalOperationParameters() { ParameterBuilder ticketPar = new ParameterBuilder(); List<Parameter> pars = new ArrayList<Parameter>(); ticketPar.name("Token") .description("User Token") .modelRef(new ModelRef("string")) .parameterType("header") .required(false); pars.add(ticketPar.build()); return pars; } ApiInfo ApiInfo () {return new ApiInfoBuilder().title(" Swagger Test ").description("This is to show API description") Apache 2.0 license (" "). LicenseUrl (" http://www.apache.org/licenses/LICENSE-2.0.html "). TermsOfServiceUrl (" ") .version(VERSION) .build(); }}Copy the code

3. Access

Launch the background code and enter the path http://localhost:port/ project name /docs.html in your browser web page

The project in the path is named as the contents in the configuration file server.servlet.context-path=/***. If the item is not configured in the configuration file, this level directory is not included

Note: If there are interceptors in the project, they may affect swagger access and require additional processing, which will not be explained here. Also, sometimes for versionor other reasons, the last level of the access path may be doc.html or swagger-ui.html, depending on the situation.