• 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/**"/>


  • 相关阅读:
    Array
    ie兼容
    webpack----entry
    webpack——html-webpack-plugin
    webpack 报错 path is not defind
    PHPStorm自动压缩YUI Compressor配置
    fly
    Math.max得到数组中最大值
    简单html弹窗
    js中页面跳转(href)中文参数传输方式
  • 原文地址:https://www.cnblogs.com/liuruilongdn/p/9330932.html
Copyright © 2020-2023  润新知