Bootstrap

swagger基本使用(前后端交互在线接口文档)

swagger(前后端交互在线接口文档)

1.导入依赖

<!--swagger 依赖-->
<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>

2.编写配置类

(4.配置开启环境)

(demo06/swagger/SwaggerConfig.java)

package com.fzy.demo06.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.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;

@Configuration
@EnableSwagger2    //开启swagger在线接口文档
public class SwaggerConfig {


    private ApiInfo apiInfo() {


        Contact contact = new Contact("蒂塔","https://github.com/DiTa2354","[email protected]");


        return new ApiInfoBuilder().title("博丽神社运营平台接口文档")
                .description("博丽神社运营平台接口文档描述")
                .version("1.6.0")
                .contact(contact)
                .build();


    }
    @Bean
    public Docket createRestApi(Environment environment) {

        Profiles profiles = Profiles.of("dev","sit");		//开启swagger文档的环境有
        boolean enable = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(enable)   //是否开启swagger文档
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.fzy.demo06.controller")) //这里写的是API接口所在的包位置
                .paths(PathSelectors.any())
                .build();
    }
}

3.Swagger注解

@Api( description = “登录控制器”)

@ApiOperation(“文件上传”)

@ApiParam(“文件,注意:要求input的name必须为head”)

@ApiModel(“响应结果”)

@ApiModelProperty(“表示操作结果,如果为true表示操作成功否则就是失败”)

@Api( description = "登录控制器") //描述控制器类的
@ApiOperation("登录")            //描述方法的
@ApiParam("用户账户dto")         //描述参数的


//如果参数是一个entity,描述entity
@Data
@ApiModel("用户账户实体类")
public class UserDTO {
    @ApiModelProperty("用户名")
    private String username;
    @ApiModelProperty("密码")
    private String password;
}
;