swagger使用方法

swagger使用方法

    • 1.需要的Maven依赖
    • 2.配置swagger
    • 3.结尾![启动完成](https://img-blog.csdnimg.cn/20200610125208800.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNjk2NjMz,size_16,color_FFFFFF,t_70)

1.需要的Maven依赖

swagger-ui:是swagger的视图里面包含了ui界面

	
		
            io.springfox
            springfox-swagger-ui
            2.9.2
        
        
            io.springfox
            springfox-swagger2
            2.9.2
        

2.配置swagger

配置swagger的配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
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;

import java.util.ArrayList;


@Configuration
//开启swagger
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docker(Environment environment){
        //设置要显示的swagger的环境
        Profiles profile = Profiles.of("dev","test");
        //通过监听的环境判断swagger的环境  这个可以判断是否是开发环境
        boolean flag = environment.acceptsProfiles(profile);
        
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //是否启用swagger  true是开启
                //.enable(false)
                .select()
                //扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.*****.controller"))
                //过滤什么路径
                //.paths(PathSelectors.ant(""))
                .build();
    }

    //作者信息
    public static final Contact DEFAULT_CONTACT = new Contact("***", "http", "***********@qq.com");
    private ApiInfo apiInfo(){
        return new ApiInfo(
            "标题",
                "哈哈哈哈",
                "v1.0",
                "我的网址",
                DEFAULT_CONTACT,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        );
    }
}

运行程序http://localhost:8080/swagger-ui.html

3.结尾swagger使用方法_第1张图片

你可能感兴趣的:(技术专区)