• springboot 集成 swagger


    1.  首先配置swaggerConfig
    package com.lixcx.lismservice.config;

    import com.lixcx.lismservice.format.CustomFormatterRegistrar;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.format.FormatterRegistry;
    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.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;



    @Configuration
    @EnableWebMvc
    @EnableSwagger2
    public class SwaggerConfig extends WebMvcConfigurerAdapter {

    @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/");*/

    registry.addResourceHandler("/**")
    .addResourceLocations("classpath:/static/");
    registry.addResourceHandler("swagger-ui.html")
    .addResourceLocations("classpath:/META-INF/resources/");
    registry.addResourceHandler("/webjars/**")
    .addResourceLocations("classpath:/META-INF/resources/webjars/");
    super.addResourceHandlers(registry);
    }

    @Bean
    public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
    .apiInfo(apiInfo())
    .select()
    .apis(RequestHandlerSelectors.basePackage("com.lixcx.lismservice.controller"))
    .paths(PathSelectors.any())
    .build();
    }
    private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
    .title("APP RESTful APIs")
    .description("在线文档:http://www.xxx.xxx")
    .termsOfServiceUrl("http://www.xxxx.com.cn/")
    .contact("AndyBao")
    .version("0.5.1")
    .build();
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
    new CustomFormatterRegistrar("value").registerFormatters(registry);
    }

    }

    2. 在springboot websecurityConfig.java中 增加 相关的通过规则

    3. gradle方式: 

    build.gradle文件中 增加以下jar

     compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.7.0'
       compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.7.0'

    4.在controller 中的接口方法上增加@ApiOperation注解 

    5. http://ip:port/app/swagger-ui.html 

     
  • 相关阅读:
    关于GitHub推送时发生Permission denied (publickey)的问题
    线性模型——机器学习(西瓜书)读书笔记
    梯度下降算法的简单理解
    PRML学习笔记第一章
    python函数学习之装饰器
    机器学习 概论
    Mybatis
    Nginx 常用配置清单
    接口,抽象类
    IntelliJ IDEA打war包
  • 原文地址:https://www.cnblogs.com/xifenglou/p/8329671.html
Copyright © 2020-2023  润新知