Springboot集成Swagger2

引入依赖

<!-- swagger2 配置 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.4.0</version>
        </dependency>

引入 bootstrap ui依赖

<dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.6</version>
        </dependency>

配置bean

package com.imooc.config;


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;

@Configuration
@EnableSwagger2
public class Swagger2 {
    // http://localhost:8088/swagger-ui.html
    // http://localhost:8088/doc.html
    // 配置Swagger2  核心配置
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)  //指定api类型
                .apiInfo(apiInfo())                     //用户定义api文档汇总信息
                .select().apis(RequestHandlerSelectors.basePackage("com.imooc.controller")) //所有包下面的controller
                .paths(PathSelectors.any())             //所有路径
                .build();
    }


    private ApiInfo apiInfo(){
        return new ApiInfoBuilder().title("天天吃货电商平台 吃货api")
                .contact(new Contact("imooc","imooc.com","imooc@com"))
                .description("天天吃货api文档").version("0.0.1")
                .termsOfServiceUrl("www.baidu.com").build();
    }

}
Swagger2

 

接口注释方法:

Controller注释:

@Api(value = "注册登录",tags = "用于注册登录相关的接口")

Mothod注释:

@ApiOperation(value = "判断username是否存在",notes = "判断username是否存在")

BeanClass注释:

@ApiModel(value = "用户对象BO",description = "从客户端,由用户传入的数据封装在此entity中")

BeanProperty注释:

@ApiModelProperty(value = "用户名",name = "username",example = "示例")

 

posted @ 2021-06-25 08:54  苦心明  阅读(45)  评论(0)    收藏  举报