swagger的使用

springboot集成swagger使用

  • Swagger UI是一款RESTFUL接口的文档在线自动生成+功能测试功能软件。

  • Swagger-UI 的官方地址:http://swagger.io/

  • Github上的项目地址: https://github.com/swagger-api/swagger-ui

  • 官方提供的demo地址:http://petstore.swagger.io/

1、引入依赖
<!--swagger依赖1-->
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger2</artifactId>
	<version>2.9.2</version>
</dependency>
<!--swagger依赖2-->
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger-ui</artifactId>
	<version>2.9.2</version>
</dependency>
2、配置swagger
1. 在启动类加注解
@SpringBootApplication
@MapperScan("com.smile.dao")
@EnableSwagger2  // 加启用swagger注解
public class BlogApplication {

	public static void main(String[] args) {
		SpringApplication.run(BlogApplication.class, args);
	}

}

测试是否成功:启动项目访问http://localhost:8080/swagger-ui.html,若能打开swagger界面说明配置成功。
在这里插入图片描述

2. 编写swagger配置类
/**
 * @Author: smile
 * @Date: 2020/2/10
 */
@Configuration
public class SwaggerConfig {
    
    /**
     * 配置swagger的docket实例
     * @return
     */
    @Bean
    public Docket docket(Environment environment) {
        // 获取要显示swagger的yml配置环境
        Profiles profiles = Profiles.of("dev");
        // 判断是否是当前获取到的环境,用来控制是否启用swagger
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                // 配置swagger显示信息
                .apiInfo(apiInfo())
                // 配置分组,可以配置多个,注入多个docket即可,但不能重名。
                .groupName("smilevers")
                // 配置是否开启swagger
                .enable(flag)
                .select()
                // 配置扫描的包
                .apis(RequestHandlerSelectors.basePackage("com.smile.controller"))
                // 过滤要扫描的路径
                .paths(PathSelectors.ant("/**"))
                .build();
    }
    
    /**
     * 配置swagger显示信息类
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfo(
                "个人博客系统"
                , "", "V1.1"
                , ""
                , new Contact("", "", "")
                , "Apache 2.0"
                , ""
                , new ArrayList());
    }
}
3. 实体类配置

只要返回值中有实体类,moudle中可以显示
实体类注释

@ApiModel("标签")  // 实体类上的注释
@ApiModelProperty("标签名")  //实体类中字段注释
4. 接口controller注释
@Api(tags = "前端首页展示")  // controller类上的注解
@ApiOperation("获取归档")  // controller类中的方法上的注解
@ApiParam("当前页码")  // 参数上的注解
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值