zoukankan      html  css  js  c++  java
  • sprinvmvc整合swagger实现实时接口信息展示

    1、pom.xml引入swagger插件

    <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.5.0</version>
    </dependency>
    <!-- 提供三种UI展示方式 -->
    <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.5.0</version>
    </dependency>
    <dependency>
    <groupId>com.github.caspar-chen</groupId>
    <artifactId>swagger-ui-layer</artifactId>
    <version>0.0.4</version>
    </dependency>
    <dependency>
    <groupId>com.drore.cloud</groupId>
    <artifactId>swagger-bootstrap-ui</artifactId>
    <version>1.4</version>
    </dependency>

    2、配置spring-mvc.xml注入swagger扫描配置

    <!-- 整合swagger-UI -->
    <bean class="com.ucredit.bc.common.filter.MySwaggerConfig"/>

    3、创建MySwaggerConfig.java 配置中心实例

    package com.**.common.filter;

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    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;

    @EnableWebMvc
    @EnableSwagger2
    @ComponentScan(basePackages = {"com.**"})
    @Configuration
    public class MySwaggerConfig extends WebMvcConfigurerAdapter {

    @Bean
    public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
    .apiInfo(apiInfo())
    .select()
    .apis(RequestHandlerSelectors.basePackage("com.**"))
    .paths(PathSelectors.any())
    .build();
    }

    private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
    .title("**系统数据接口文档")
    .termsOfServiceUrl("https://test-**.com")
    .contact("**@**.com")
    .version("0.0.1")
    .description("**系统数据接口文档")
    .build();
    }

    }

    注:若工程使用的spring Shiro等安全拦截 需进行静态资源过滤,
    /v2/** = anon
    /doc.html = resource
    /menu.json = resource
    /docs.html = resource
    /webjars/** = anon
    /swagger-resources/** = anon
    /swagger-ui.html = resource
  • 相关阅读:
    搭建聊天机器人Bot Framework
    UsernamePasswordAuthenticationToken
    在JavaWeb项目中URL中字符串加密解密方案
    python自定义库文件路径
    difference between collection and association mapping in mybatis 3
    statpot:使用mongo+bootstrap+highcharts做统计报表
    Android仿微信SlideView聊天列表滑动删除效果
    Android Studio 运行、编译卡死的解决办法
    Android wear
    android实现json数据的解析和把数据转换成json格式的字符串
  • 原文地址:https://www.cnblogs.com/Gent-Wang/p/8944147.html
Copyright © 2011-2022 走看看