• 8.【原创】Spring Mvc配置Swagger+swagger-bootstrap-ui生成日志服务


    网上无意中看到swagger日志插件和swagger-bootstrap-ui感觉蛮实用的。这样能动态生成对接文档,还能在线测试接口,能直观看到接口响应信息的格式,对于前端人员来说比较方便,同时对于服务器开发人员来说也比较不错,能够统一格式,且不用专门切换到单独的文档文件,还能直接生成.md文件,还是比较方便的,配置期间,也出现了一些问题,特此记录一下,也方便以后使用的时候方便查看。

    1.引入maven依赖

    <!--5.1.1swagger文档依赖-->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>${swagger.version}</version>
            </dependency>
            <!--5.1.2.swagger的可视化展示依赖:第三方,改版后的最后一个版本-->
            <dependency>
                <groupId>com.github.xiaoymin</groupId>
                <artifactId>swagger-bootstrap-ui</artifactId>
                <version>1.9.6</version>
            </dependency>

    2.1书写配置类,并注入到spring mvc核心容器(注解和xml方式二选一,此处我用的是xml方式)

    package cn.drip.dripblog.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.Contact;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    /**
     * @Author: Chunhai.Hu
     * @Date: 2020/9/10 0010 10:26
     * @Last Modified by: Chunhai.Hu
     * @Last Modified time: 2020/9/10 0010 10:26
     * @Desc: Swagger文档配置类
     */
    @EnableSwagger2
    @EnableWebMvc
    public class SwaggerConfig {
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("cn.drip.dripblog.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("dripblog水滴博客API说明")
                    .description("dripblog水滴博客API说明文档,如有疑问请邮件联系作者。")
                    .termsOfServiceUrl("http://localhost:8000/")
                    .contact("viphuchunhai@163.com")
                    .version("1.0.0")
                    .build();
        }
    }

    2.2.注入springmvc核心容器

    <!--注入swagger文档配置类-->
    <bean id="swaggerConfig" class="cn.drip.dripblog.config.SwaggerConfig"></bean>

    3.1.配置文档访问的页面路由(xml和配置类方式二选一,此处我使用配置xml方式)

    <!--配置swagger-bootstrap-ui访问的静态资源-->
        <mvc:resources mapping="doc.html" location="classpath:/META-INF/resources/"/>
        <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

    3.2.注解方式(使用了3.1的方式就不要写3.2的了)

    package cn.drip.dripblog.config;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    /**
     * @Author: Chunhai.Hu
     * @Date: 2020/9/10 0010 12:40
     * @Last Modified by: Chunhai.Hu
     * @Last Modified time: 2020/9/10 0010 12:40
     * @Desc: 配置swagger-bootstrap-ui访问的静态资源:
     * @使用xml和配置类两种方式,选其一即可
     */
    @Configuration
    public class SwaggerUiConfig implements WebMvcConfigurer {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    }

    4.配置web.xml中的404页面(CustomizeDispatcherServlet为自定义请求处理器,详见:https://www.cnblogs.com/Nick-Hu/p/13650453.html(3.2点))

      <!--步骤4.1:配置初始页和异常处理页面-->
      <servlet-mapping>
        <servlet-name>CustomizeDispatcherServlet</servlet-name>
        <url-pattern>/static/view/404.html</url-pattern>
      </servlet-mapping>
      <error-page>
        <error-code>404</error-code>
        <location>/static/view/404.html</location>
      </error-page>
      <servlet-mapping>
        <servlet-name>CustomizeDispatcherServlet</servlet-name>
        <url-pattern>/static/view/405.html</url-pattern>
      </servlet-mapping>
      <error-page>
        <error-code>405</error-code>
        <location>/static/view/405.html</location>
      </error-page>
      <servlet-mapping>
        <servlet-name>CustomizeDispatcherServlet</servlet-name>
        <url-pattern>/static/view/500.html</url-pattern>
      </servlet-mapping>
      <error-page>
        <error-code>500</error-code>
        <location>/static/view/500.html</location>
      </error-page>

    5.访问测试:访问地址:http://{ip}:{port}/{projectName}/doc.html

    6.配置参考:https://doc.xiaominfo.com/(swagger-bootstrap-ui官网)

  • 相关阅读:
    优先队列
    BFS和图的最短路径 279,127,126
    opencv常用函数
    图形图像概念
    缓存
    主板
    显卡
    cpu
    vs配置opencv(只需一次)
    附加依赖项、库目录、包含目录
  • 原文地址:https://www.cnblogs.com/Nick-Hu/p/13650942.html
Copyright © 2020-2023  润新知