1. Create a SpringBoot project

2. 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> The < version > 2.9.2 < / version > < / dependency >Copy the code

4. Configuration swagger

package com.liy.config; import org.springframework.context.annotation.Configuration; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * @ClassName: swaggerConfig * @Description: * @author: liYue * @date: 2021/6/13 11:12 */ @configuration @enableswagger2 // EnableSwagger2 public class swaggerConfig {}Copy the code

5. Test it outhttp://localhost:8080/swagger-ui.html

6. Modify some configurations

package com.liy.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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; import java.util.ArrayList; import static springfox.documentation.service.ApiInfo.DEFAULT_CONTACT; /** * @ClassName: swaggerConfig * @Description: * @author: liYue * @date: 2021/6/13 11:12 */ @configuration @enableswagger2 // EnableSwagger2 public class swaggerConfig {//swagger docket bean instance @Bean public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()); } @bean public ApiInfo ApiInfo (){// @bean public ApiInfo ApiInfo (){// @bean public ApiInfo ApiInfo (){// @bean public ApiInfo ApiInfo (); Return new ApiInfo(" SwaggerAPI document ", "Apache 2.0", "1.0", "URN: TOS ", DEFAULT_CONTACT, "Apache 2.0", "Http://www.apache.org/licenses/LICENSE-2.0", new ArrayList ()); }}Copy the code

7. You can set the interface to display and enable Swagger based on the current development environment

Copy the code

package com.liy.config;

import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Profiles; 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; / / import methods rely on the package of package/class import org. Springframework. Core. The env. The Environment;

import java.util.ArrayList;

import static springfox.documentation.service.ApiInfo.DEFAULT_CONTACT;

/ * *

  • @ClassName: swaggerConfig
  • @Description:
  • @author: liYue
  • @date: 2021/6/13 11:12

*/ @configuration@enableswagger2 // EnableSwagger2 public class swaggerConfig {

@bean public docket docket(Environment Environment) {// Sets the swagger Environment to be displayed Profiles.of ("dev","test");Copy the code

/ / by envi ronment. AcceptsProfiles determine whether you set in the environment of the environment a Boolean flag = environment. AcceptsProfiles (profiles); System.out.println(flag) ; Return new Docket (DocumentationType SWAGGER_2). / / whether the enable startup Swagger, if is False, then Swagger to the browser to access

ApiInfo (apiInfo ()). The select (). / / RequestHandlerSelectors, //basePackage: specifies the packets to be scanned. //any(): scans all. // None (): does not scan apis(RequestHandlerSelectors.basePackage("com.liy.cont")).build(); } @bean public ApiInfo ApiInfo () {// @bean public ApiInfo ApiInfo () {// @bean public ApiInfo ApiInfo () {// @bean public ApiInfo ApiInfo (); Return new ApiInfo(" SwaggerAPI document ", "Apache 2.0", "1.0", "URN: TOS ", DEFAULT_CONTACT, "Apache 2.0", "Http://www.apache.org/licenses/LICENSE-2.0", new ArrayList ()); }Copy the code

}

Copy the code