SpringBoot 整合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>

        <!-- 生成文档 Swagger -->
        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
            <version>1.4.01</version>
        </dependency>

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

第二步:编写配置文件

@Configuration
@EnableSwagger2
public class SwaggerConfig {
 
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                //页面展示信息设置
                .apiInfo(apiInfo())
                .select()
                //为当前包下controller生成API文档
                .apis(RequestHandlerSelectors.basePackage("com"))
                .paths(PathSelectors.any())
                .build();
    }
    
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Swagger在线文档")
                .description("JobOperate API")
                //联系人:名字 网址 邮箱      本地访问地址:http://localhost:8099/swagger-ui.html#/
                .contact(new Contact("JobOperate","localhost:8099","xxxx@qq.com"))
                //版本号
                .version("1.0")
                .build();
    }
}

本地访问地址:http://localhost:8099/swagger-ui.html#/

第三步:将swagger的访问路径设置白名单

    - "/swagger-ui.html/**"
    - "/webjars/**"
    - "/swagger-resources/**"
    - "/v2/**"
    - "doc.html"
    - "/error"

第四步:swagger常用注解以及含义

@Api()用于类;表示标识这个类是swagger的资源
@ApiOperation()用于方法;表示一个http请求的操作 
@ApiParam()用于方法,参数,字段说明;表示对参数的添加元数据(说明或是否必填等)
@ApiModel()用于类 ;表示对类进行说明,用于参数用实体类接收 
@ApiModelProperty()用于方法,字段 ;表示对model属性的说明或者数据操作更改 
 @ApiIgnore()用于类,方法,方法参数 ;表示这个方法或者类被忽略 
@ApiImplicitParam()用于方法 ;表示单独的请求参数 
 @ApiImplicitParams()用于方法;包含多个 @ApiImplicitParam

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值