美文网首页
springboot 之集成Swagger2

springboot 之集成Swagger2

作者: xuefly3 | 来源:发表于2019-12-20 17:26 被阅读0次

    前言

    一直没机会做spring生态圈的框架,公司选择的是一些小众的微服务,鉴于此考虑,丰富自己的技术栈,花了两天时间从网上各网站上学习了springboot一些基础知识。
    本章只介绍springboot微服务集成swagger2,用于后端工程师开发过程中调试接口。

    环境准备

    • IntelliJ IDEA
    • 前一章中搭建的微服务框架

    开始集成

    1. pom.xml中增加依赖包


      依赖包.png
            <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>
                <version>2.9.2</version>
            </dependency>
    
    1. 引入swaggerUI静态资源文件,将资源拷贝到resources/static目录下,否则会报404,找不到资源,资源下载路径见://download.csdn.net/download/xuefei2290819/12043546

      swagger静态资源.png
    2. 与DemoApplication.java同级目录新建SwaggerConfig类


      SwaggerConfig.png
    package com.example.demo;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    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;
    
    import java.util.List;
    
    /**
     * 类功能描述:<br>
     * <ul>
     * <li>类功能描述1<br>
     * <li>类功能描述2<br>
     * <li>类功能描述3<br>
     * </ul>
     * 修改记录:<br>
     * <ul>
     * <li>修改记录描述1<br>
     * <li>修改记录描述2<br>
     * <li>修改记录描述3<br>
     * </ul>
     *
     * @author xuefl
     * @version 5.0 since 2019-12-19
     */
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig implements WebMvcConfigurer {
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");
            // 解决 SWAGGER 404报错
            registry.addResourceHandler("/static/index.html").addResourceLocations("classpath:/META-INF/resources/static/");
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    
        @Override
        public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    
        }
    
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())//生成文档的api对象定义
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))//扫描生成文档的包路径
                    //.paths(PathSelectors.ant("/*Api/*"))//生成文档的类访问路径,就是controller类里@RequestMapping("orderApi")
                    .paths(PathSelectors.any())
                    .build();
            //.host(Host);//配置swagger前缀
        }
    
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("springboot-demo构建restful接口api")//文档标题
                    .description("此API提供接口调用")//文档说明
                    .version("2.0").build();//版本号
        }
    }
    
    1. DemoApplication中增加@EnableSwagger2注解


      EnableSwagger2.png
    package com.example.demo;
    
    import lombok.extern.slf4j.Slf4j;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @SpringBootApplication
    @EnableSwagger2
    @MapperScan("com.example.demo.mapper")
    @EnableScheduling
    @EnableTransactionManagement
    @Slf4j
    public class DemoApplication {
    
        public static void main(String[] args) {
            log.info("DemoApplication run enter...");
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }
    
    1. 运行DemoApplication,启动服务,访问 http://localhost:8081/api/
      运行结果.png

    相关文章

      网友评论

          本文标题:springboot 之集成Swagger2

          本文链接:https://www.haomeiwen.com/subject/atrinctx.html