1、创建springboot的web项目,引入maven依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
2、swagger配置类
主要配置扫描哪些包下的类,也就是具体controller
package com.example.demo.config;
import oracle.jrockit.jfr.events.RequestableEventEnvironment;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.stereotype.Controller;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
import java.util.function.Predicate;
import static springfox.documentation.service.ApiInfo.DEFAULT_CONTACT;
@Configuration
@EnableOpenApi
public class SwaggerConfig {
@Bean
public Docket docket(Environment environment){
//指定环境类型
Profiles profile = Profiles.of("dev");
//判断是否为dev环境
boolean b = environment.acceptsProfiles(profile);
return new Docket(new DocumentationType("swagger", "2.0"))
.apiInfo(getapiInfo())
.enable(b) //是否启用
.select()
//BasePackage:指定扫描的包
//none:不扫描
//any:扫描全部
//.apis(RequestHandlerSelectors.none())
//.apis(RequestHandlerSelectors.withClassAnnotation(Controller.class))
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.build();
}
public ApiInfo getapiInfo(){
Contact contact = new Contact("作者信息", "https://blog.csdn.net/qq_41048799?spm=1001.2101.3001.5343", "");
return new ApiInfo(
"myApi 自定义",
"因为不想跑,所以才去跑",
"1.0",
"https://blog.csdn.net/qq_41048799?spm=1001.2101.3001.5343",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
3、启动springboot,访问http://localhost:8080/swagger-ui/index.html