spring boot项目添加swagger 2.7.0(只需两步操作)

2021-07-08 05:04

阅读:534

标签:color   ORC   type   扫码   pre   cto   bsp   contex   就是   

1.pom.xml引入swagger 2.7的jar包

        
        dependency>
            groupId>io.springfoxgroupId>
            artifactId>springfox-swagger2artifactId>
            version>2.7.0version>
        dependency>
        dependency>
            groupId>io.springfoxgroupId>
            artifactId>springfox-swagger-uiartifactId>
            version>2.7.0version>
        dependency>
        

2.SwaggerConfig.class(放在可以被spring扫码到的地方)

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;

/**
 * @author TangZedong
 * @apiNote swagger2配置文件
 * @since 2018/9/4 10:46
 */
@Configuration
@EnableSwagger2
@ConfigurationProperties
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .forCodeGeneration(true)
.groupName("指定group的名称,groupName不能重复") .select() .apis(RequestHandlerSelectors.basePackage(
"这里是你需要扫描的包路径")) //过滤生成链接 .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } /** * the api info * * @return api info */ private ApiInfo apiInfo() { return new ApiInfoBuilder() .license("Apache License Version 2.0") .title("blogspot") .description("api docs") .contact(new Contact("tangzedong", "https://www.cnblogs.com/HackerBlog/", "tangzedong.programmer@gmail.com")) .version("1.0") .build(); } }

然后所有的操作就完成了,是不是很简单?就是这么简单

然后启动spring boot服务器,在网页上输入网址:http://localhost:8080/swagger-ui.html 你就可以看见swagger的页面了

技术分享图片

 

spring boot项目添加swagger 2.7.0(只需两步操作)

标签:color   ORC   type   扫码   pre   cto   bsp   contex   就是   

原文地址:https://www.cnblogs.com/HackerBlog/p/9584513.html


评论


亲,登录后才可以留言!