SpringBoot配置swagger

引入依赖

<properties>
        <swagger.version>2.9.2</swagger.version>
        <swagger-ui.version>2.9.2</swagger-ui.version>
        <swagger-annotation-models-version>1.5.21</swagger-annotation-models-version>
</properties>

<dependencies>
	<dependency>
         <groupId>io.swagger</groupId>
         <artifactId>swagger-annotations</artifactId>
         <version>${swagger-annotation-models-version}</version>
     </dependency>

     <dependency>
         <groupId>io.swagger</groupId>
         <artifactId>swagger-models</artifactId>
         <version>${swagger-annotation-models-version}</version>
     </dependency>

     <!-- 引入swagger -->
     <dependency>
         <groupId>io.springfox</groupId>
         <artifactId>springfox-swagger2</artifactId>
         <version>${swagger.version}</version>
         <exclusions>
             <exclusion>
                 <groupId>io.swagger</groupId>
                 <artifactId>swagger-annotations</artifactId>
             </exclusion>
             <exclusion>
                 <groupId>io.swagger</groupId>
                 <artifactId>swagger-models</artifactId>
             </exclusion>
         </exclusions>
     </dependency>

     <!-- 引入swagger-ui -->
     <dependency>
         <groupId>io.springfox</groupId>
         <artifactId>springfox-swagger-ui</artifactId>
         <version>${swagger-ui.version}</version>
     </dependency>
</dependencies>            

核心配置类

package com.ddf.boot.common.core.config;

import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swagger的配置类
 * <p>
 * 项目启动后文档访问地址为[ip]:[port]/[context-path]/swagger-ui.html
 * <p>
 * _ooOoo_
 * o8888888o
 * 88" . "88
 * (| -_- |)
 * O\ = /O
 * ___/`---'\____
 * .   ' \\| |// `.
 * / \\||| : |||// \
 * / _||||| -:- |||||- \
 * | | \\\ - /// | |
 * | \_| ''\---/'' | |
 * \ .-\__ `-` ___/-. /
 * ___`. .' /--.--\ `. . __
 * ."" '< `.___\_<|>_/___.' >'"".
 * | | : `- \`.;`\ _ /`;.`/ - ` : | |
 * \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 * `=---='
 * .............................................
 * 佛曰:bug泛滥,我已瘫痪!
 *
 * @author dongfang.ding
 * @date 2019/5/28 14:09
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {

        ParameterBuilder authorizationPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        // 添加自定义请求头,这样在生成swagger文档的时候,每次调试都会有这几个请求头
        pars.add(authorizationPar.name("Authorization")
                .description("token信息")
                .modelRef(new ModelRef("string"))
                // 自定义请求头名称
                .parameterType("header")
                // 请求头的默认值
                .defaultValue("Bearer ")
                // 是否必传
                .required(false)
                .build());
        // 还是添加请求头,将请求方式默认为application/json       
        pars.add(authorizationPar.name("Content-Type")
                .description("Content-Type")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .defaultValue("application/json")
                .required(true)
                .build());

        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .select()
                // 在这里配置需要扫描的项目的包的路径
                .apis(RequestHandlerSelectors.basePackage("com"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("spring-boot的脚手架工程").description(
                "使用spring-boot集成一些常用框架,来完成一个基本框架的搭建,可以用来作为实际项目开发的基准").version("1.0").build();
    }
}

这里只说配置,具体使用相关的 注解这里就不写了,网上有更详细的文档,写这篇的目的是因为swagger之前使用的时候一些包的依赖有问题,需要排除之后重新升级依赖包,当然具体原因及不得了,这里分享在这里。还有就是生成的文档可以自定义添加请求头,这个还是很方便的,配置方法也分享在上面。

当然这里还是要推荐另外一个在线文档yapi.
具体特性可以参考官方,避免了swagger大量的侵入注解,在使用的时候只要使用标准的javadoc 注释,在配合一些上传插件, 就可以做到一键上传,且在线调试功能也比swagger丰富很多。
当然之前也分享过一个这方面的文档,里面说明了如何去搭建以及去配置一键上传接口等方面的内容。搭建自己的服务器环境之安装项目文档工具YApi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值