快速入门Swagger

1.创建web项目

2.引入依赖

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

<dependency>
     <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

遇到一个bug:最开始使用的是最新版本3.0.0,两个依赖都是。

无法访问localhost:8080/swagger-ui.html

降了版本就可以了

3.写接口

4.创建一个Swagger配置类:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    
    
    @Bean    
    public Docket getDocket1(Environment environment){
    
    
        //再创建一个分组
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");  
    }
    @Bean
    public Docket getDocket(Environment environment){
    
    
        //根据环境配置决定能不能用swagger,这里表示测试环境可用
        Profiles profiles = Profiles.of("test");
        boolean b = environment.acceptsProfiles(profiles);
        System.out.println(b);
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .groupName("向培")    //分组
                .enable(b)          //是否使用swagger
                .select()  
             .apis(RequestHandlerSelectors.basePackage("com.xp.swagger2.controller"))   			//哪些包下可用Swagger
                .build();
    }

    //个人信息
    public Contact concat = new Contact("xiangpei", "www.baidu.com", "[email protected]");
	//Swagger界面的信息
    private ApiInfo apiInfo(){
    
    
        return new ApiInfo("向培学Swagger", "test", "1.0", "urn:tos", concat, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
    }
}

image-20211003224001899

如果接口中使用了实体类,该实体类就会在Model区显示出来;没有使用是不会加载的

几个常用说明注解:

@ApiModel(“用户”) 实体类说明

@ApiModelProperty 属性说明

@ApiOperation(“向培写的helloworld”) 接口说明

Guess you like

Origin blog.csdn.net/qq_42682745/article/details/120613554