栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

swagger

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

swagger

maven坐标
		
			io.springfox
			springfox-swagger2
			2.8.0
		
		
			com.github.xiaoymin
			swagger-bootstrap-ui
			1.9.3
		
配置类
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
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;


@SpringBootConfiguration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class Swagger2Config {

    private static final Logger log = LoggerFactory.getLogger(Swagger2Config.class);

    @Value("${swagger2.show}")
    private Boolean show;

    @Value("${swagger2.group-name}")
    private String groupName;

    @Value("${swagger2.base-package}")
    private String basePackage;

    @Value("${swagger2.title}")
    private String title;

    @Value("${swagger2.description}")
    private String description;

    @Value("${swagger2.terms-of-service-url}")
    private String termsOfServiceUrl;

    @Value("${swagger2.contact-name}")
    private String contactName;

    @Value("${swagger2.contact-url}")
    private String contactUrl;

    @Value("${swagger2.contact-email}")
    private String contactEmail;

    @Value("${swagger2.version}")
    private String version;

    @Bean
    public Docket orderRestApi() {
        Docket docket = new Docket(documentationType.SWAGGER_2)
                .enable(show)
                .groupName(groupName)
                .apiInfo(apiInfo())
                .useDefaultResponseMessages(false)
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .termsOfServiceUrl(termsOfServiceUrl)
                .contact(new Contact(contactName, contactUrl, contactEmail))
                .version(version)
                .build();
    }

}

3 yml
swagger2:
  show: true
  group-name: ${spring.application.name}
  base-package: com.czn
  title: FileSystem服务接口文档
  description: FileSystem服务接口文档
  terms-of-service-url: http://127.0.0.1:${server.port}
  contact-name: czn
  contact-url: xxx
  contact-email: xxx
  version: V1.0
4 发放接口
@SpringBootApplication
@ConditionalOnClass(SpringfoxWebMvcConfiguration.class)
public class SwaggerBootstrapUiDemoApplication  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/");
    }
}

5 访问地址 host+port+doc.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/679160.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号