zoukankan      html  css  js  c++  java
  • springmvc集成swagger

    1、保证项目为maven项目

    2、导入jar包依赖

    <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.7.0</version>
    </dependency>
    <dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.7.0</version>
    </dependency>

    3、在src/com/XXX/system项目下添加SwaggerConfig


    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    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.service.Contact;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;

    @Configuration
    @EnableWebMvc
    @EnableSwagger2
    public class SwaggerConfig extends WebMvcConfigurerAdapter {
    private static final String PROD_ENV = "PRODS";


    private String serverEnv = "DEV";
    @Bean
    public Docket customDocket() {
    if(PROD_ENV.equals(serverEnv)) {
    return new Docket(DocumentationType.SWAGGER_2)
    .apiInfo(apiInfoOnline()).select()
    .apis(RequestHandlerSelectors.basePackage("com.XXX.system.controller"))(注意配置这个地址)
    .paths(PathSelectors.none())
    .build();
    } else {
    return new Docket(DocumentationType.SWAGGER_2)
    .apiInfo(apiInfo())
    .select()
    .apis(RequestHandlerSelectors.any())
    .paths(PathSelectors.any())
    .build();
    }
    }

    private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
    .title("device data micro-service")
    .description("device data micro-service API")
    .license("").licenseUrl("").termsOfServiceUrl("")
    .version("1.0.0")
    .contact(new Contact("js11b", "", "")).build();
    }

    private ApiInfo apiInfoOnline() {
    return new ApiInfoBuilder().build();
    }

    /**
    * enable swagger-ui feature
    */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("swagger-ui.html")
    .addResourceLocations("classpath:/META-INF/resources/");

    registry.addResourceHandler("/webjars/**")
    .addResourceLocations("classpath:/META-INF/resources/webjars/");

    }

    }



    4、在spring-mvc.xml中加

    <bean class="com.XXXX.system.SwaggerConfig"/>

    5、对应的拦截器排除的路径

    <mvc:exclude-mapping path="/v2/api-docs"/>
    <mvc:exclude-mapping path="swagger-resources/configuration/ui"/>
    <mvc:exclude-mapping path="/swagger-resources/**"/>
    <mvc:exclude-mapping path="/swagger-resources/configuration/security"/>
    <mvc:exclude-mapping path="/swagger-ui.html"/>
    <mvc:exclude-mapping path="/webjars/springfox-swagger-ui/*"/>
    <mvc:exclude-mapping path="/webjars/**"/>


  • 相关阅读:
    一些坑爹的错误
    鼠标键盘钩子捕获(初版)
    【Win10】我们无法更新系统保留的分区
    实验一:c++简单程序设计(1)
    《Java 8 in Action》Chapter 1:为什么要关心Java 8
    《Java 8 in Action》Chapter 2:通过行为参数化传递代码
    Java集合类综合
    Java内存模型
    你真的会阅读Java的异常信息吗?
    Java常用异常整理
  • 原文地址:https://www.cnblogs.com/liuruilongdn/p/9330932.html
Copyright © 2011-2022 走看看