springboot整合swagger教程

1. Application

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableCaching
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
	//配置Swagger信息
	private ApiInfo apiInfo() {
		return new ApiInfo(
				"Api Documentation",
				"Swagger API文档",
				"1.0",
				null,
				null,
				null,
				null,
				new ArrayList<VendorExtension>());
	}

	//配置Swagger的Docket的bean实例
	@Bean
	public Docket docket(Environment environment) {
		//设置要配置的Swagger环境
		Profiles profiles = Profiles.of("dev");
//        Profiles profiles = Profiles.of("test");
		//通过environment.acceptsProfiles判断是否处在自己设定的环境中
		boolean flag = environment.acceptsProfiles(profiles);

		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())//配置Swagger信息
				.enable(flag) //通过flag判断是否开启
				.select()
				/**
				 * apis():指定扫描的接口
				 *  RequestHandlerSelectors:配置要扫描接口的方式
				 *       basePackage:指定要扫描的包
				 *       any:扫面全部
				 *       none:不扫描
				 *       withClassAnnotation:扫描类上的注解(参数是类上注解的class对象)
				 *       withMethodAnnotation:扫描方法上的注解(参数是方法上的注解的class对象)
				 */
				.apis(RequestHandlerSelectors.basePackage("com.yt.project.webapp.mgr.controller"))
				/**
				 * paths():过滤路径
				 *  PathSelectors:配置过滤的路径
				 *      any:过滤全部路径
				 *      none:不过滤路径
				 *      ant:过滤指定路径:按照按照Spring的AntPathMatcher提供的match方法进行匹配
				 *      regex:过滤指定路径:按照String的matches方法进行匹配
				 */
				.paths(PathSelectors.ant("/**"))
				.build();
	}

	@Bean
	public UiConfiguration uiConfig() {
		return UiConfigurationBuilder.builder()
//                .deepLinking(true)
//                .displayOperationId(false)
//                 隐藏UI上的Models模块
//                .defaultModelsExpandDepth(-1)
//                .defaultModelExpandDepth(-1)
//                .defaultModelRendering(ModelRendering.MODEL)
//                .displayRequestDuration(false)
//                .docExpansion(DocExpansion.NONE)
//                .filter(false)
//                .maxDisplayedTags(null)
//                .operationsSorter(OperationsSorter.ALPHA)
//                .showExtensions(false)
//                .tagsSorter(TagsSorter.ALPHA)
//                .validatorUrl(null)
				.build();
	}
}

2.pom添加依耐

	<dependency>
		<groupId>com.spring4all</groupId>
		<artifactId>swagger-spring-boot-starter</artifactId>
		<version>1.9.1.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>com.github.xiaoymin</groupId>
		<artifactId>swagger-bootstrap-ui</artifactId>
		<version>1.9.6</version>
	</dependency>

3. 访问地址: http://localhost:8080/doc.html
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10000guo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值