swagger配置笔记

依赖

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.9.2</version>
			<exclusions>
				<exclusion>
					<artifactId>swagger-models</artifactId>
					<groupId>io.swagger</groupId>
				</exclusion>
				<exclusion>
					<artifactId>swagger-annotations</artifactId>
					<groupId>io.swagger</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>io.swagger</groupId>
			<artifactId>swagger-models</artifactId>
			<version>1.5.22</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.github.xiaoymin/swagger-bootstrap-ui -->
		<dependency>
			<groupId>com.github.xiaoymin</groupId>
			<artifactId>swagger-bootstrap-ui</artifactId>
			<version>1.9.5</version>
		</dependency>

config配置

package com.mc.jewelrybespoke.app.config;

import org.springframework.beans.factory.annotation.Value;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@Configuration
public class SwaggerConfig {
	// 是否开启swagger,正式环境一般是需要关闭的,可根据springboot的多环境配置进行设置
	@Value(value = "${swagger.enabled:true}")
	private Boolean swaggerEnabled;

	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
				// 是否开启
				.enable(swaggerEnabled).select().apis(RequestHandlerSelectors.basePackage("com.mc.jewelrybespoke.app.appcontroller"))
				.paths(PathSelectors.any()).build();
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("纯定制API").version("1.0.0").build();
	}
}

范围路径: http://h o s t : {host}:host:{port}/doc.html
The default access address of the swagger-bootstrap UI is: http://h o s t : {host}:host:{port}/doc.html

注意: 如果是微服务,配置了网关,请在网关白名单里面加上路径!

效果:
在这里插入图片描述


版权声明:本文为weixin_44137464原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。