Springboot集成Swagger2

一.Swagger2是什么?

Swagger2能够动态的生成Api接口文档,特别是前后端分离的项目,方便前后端人员进行沟通.
在这里插入图片描述
在这里插入图片描述

二.Springboot集成Swagger2

1.在pom.xml中导入需要的依赖

<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.编写 SwaggerConfig配置类

package com.selenium.sdjubbs.common.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 SwaggerConfig {
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("SDJUBBS API")
                .contact(new Contact("Selenium", "https://www.github.com/wantao666", "selenium39@qq.com"))
                .description("API FOR SDJUBBS")
                .version("1.0.0")
                .termsOfServiceUrl("https://www.github.com/wantao666")
                .build();
    }

    @Bean
    public Docket createResApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select().apis(RequestHandlerSelectors.basePackage("com.selenium.sdjubbs.common.controller"))
                .paths(PathSelectors.any())
                .build();

    }
}

接着访问http://localhost:8080/swagger-ui.html就可以看见自动生成的文档了.

三.Springboot集成Swagger2的常用注解

1.@Api@ApiIgnore
@Api注释在Controller类上面,标志此Controller需要自动生成api
@ApiIgnore也注释在Controller类上面,标志此Controller不需要自动生成api

2.@ApiOperation
@ApiOperation注释在方法上,说明该方法的作用

3.@ApiImplicitParams
@ApiImplicitParams注释在方法上,里面是@ApiImplicitParam,用来说明请求参数

 @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名(长度:4-12)", required = true, example = "test"),
            @ApiImplicitParam(name = "password", value = "密码(md5加密)", required = true, example = "3a42503923d841ac9b7ec83eed03b450"),
    })

4.@ApiResponses
@ApiResponses注解在方法上,用来说明返回结果

@ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })

5.@ApiModel
@ApiModel注释在bean上,可自动生成为Model

6.@ApiModelProperty
@ApiModelProperty注释在bean的属性上,说明bean的属性参数

......

四.Springboot集成Swagger2的坑

问题: 我在某个Bean上使用了@ApiModel,但是访问http://localhost:8080/swagger-ui.html页面却没有看见这个Bean对应的Model.
解决: 只有以下两种方式会生成Model,不知道为什么

  1. 此Bean作为方法返回结果,在方法中被返回
  2. 此Bean作为方法参数,加上@RequestBody(也就是说请求格式必须是Json)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Selenium399

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值