SpringBoot整合Swagger


SpringBoot整合Swagger

前后端分离,接口需要及时协同

swagger是一个用于生成服务器接口的规范性文档、并且能够对接口进行测试的工具

1.Swagger的作用

  • 生成接口说明文档
  • 对接口进行测试

2.SpringBoot整合Swagger

  • 2.1.添加依赖(Swagger2 \ Swagger UI )
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
  • 2.2.新建config文件夹添加SwaggerConfig类,如图

在这里插入图片描述

package com.example.api.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;

/**
 * 创建Swagger2的配置类
 * @Configuration 注解:表示配置类
 * @EnableSwagger2 注解:启用Swagger2
 * 1:配置生成的文档信息
 * 2:配置生成规则
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket getDocket() {

        // 创建API信息构建器
        ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
        // 创建标题
        apiInfoBuilder.title("接口文档说明")
                // 创建描述
                .description("SpringBoot整合Swagger2")
                // 创建版本
                .version("1.0")
                // 创建联系人
                .contact(new Contact("测试", "http://www.baidu.com", "123@qq.com"));

        // 创建ApiInfo对象
        ApiInfo apiInfo = apiInfoBuilder.build();

        // 返回指定生成的文档类型
        return new Docket(DocumentationType.SWAGGER_2)
                // 生成文档的封面信息:标题、描述、版本、联系方式
                .apiInfo(apiInfo)
                // 选择需要生成文档的接口
                .select()
                // 指定生成文档的接口范围
                .apis(RequestHandlerSelectors.basePackage("com.example.api.controller"))
                // 对哪些请求路径生成文档:any:所有
                .paths(PathSelectors.any())
                .build();
    }
}

3.查看/测试生成的接口说明文档

  • 3.1.启动SpringBoot项目,访问:http://127.0.0.1:8080/swagger-ui.html
    在这里插入图片描述

  • 3.2.对接口进行测试

    • 3.2.1.找到想要测试接口,点击 Try out 按钮
      在这里插入图片描述

    • 3.2.2.输入参数,点击Execute执行
      在这里插入图片描述

    • 3.2.3.发送请求返回的响应结果
      在这里插入图片描述

4.Swagger常用注解说明

  • @API:请求类的说明 —> 用于controller类上
  • @ApiOperation:方法的说明 —> 用于方法的上面
  • @ApiImplicitParams:用在请求的方法上, 包含一组参数说明 —> 用于方法的上面
  • @ApiImplicitParam:对单个参数的说明,包含以下属性
    • name:参数名
    • value:参数的汉字说明, 解释
    • required:参数是否必须传
    • paramType:参数放在哪个地方
    • dataType: 参数类型, 默认 String, 其它值 dataType="Integer”
    • defaultValue:参数的默认值
  • @ApiResponses@ApiResponse:进行方法返回值的说明
    • code:状态码,如200、400、401等
    • message:信息,例如"请求成功”
    • response:抛出异常的类
@RestController
@Api(tags = "用户管理模块" ,value = "该参数没什么意义, 所以不需要配置")
public class UserController{

	@Resource
    private UserDAO userDAO;

    @PostMapping("getUserInfo")
	@ApiOperation(value = "根据用户名获取用户信息接口")
	@ApiImplicitParams({
       	@ApiImplicitParam(name = "userName", value = "用户名", required = true, dataType = "String", paramType = "query", defaultValue = "Tony")
  		})
	@ApiResponses({
		@ApiResponse(code = 200, message = "成功"),
       	@ApiResponse(code = 500, message = "失败")
   	})
	public User getUserInfo(String userName){
			return userDAO.getUserInfo(userName);
	}
}
  • @ApiModel:用在 JavaBean 类上,说明 JavaBean 的 用途
  • @ApiModelProperty:用在 JavaBean 类的属性上面,说明此属性的的含议
@ApiModel(value = "User", description = "用户实体类")
public class User implements Serializable{
	@ApiModelProperty(value = "用户名")
	private String userName;
    @ApiModelProperty(value = "密码")
	private String passWord;
}
  • @ApiIgnore:忽略某个接口,不生成在Swagger的接口说明文档中
  • 整体结果展示

在这里插入图片描述

5.原始页面太丑,换个页面

  • springfox-swagger-ui 的界面长这个样子,说实话,确实略显丑陋。
  • 改成改良后的 Knife4j 不仅在界面上更加优雅、炫酷,功能上也更加强大:后端 Java 代码和前端 UI 模块分离了出来,在微服务场景下更加灵活;更提供了专注于 Swagger 的增强解决方案.
<!-- 将原来的springfox-swagger2版本改成3.0.0,解决以下版本兼容性问题 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>

<!-- 将原来的springfox-swagger-ui版本改成knife4j-spring-boot-starter -->
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>3.0.3</version>
</dependency>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

你的泪丶烫伤我的脸

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

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

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

打赏作者

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

抵扣说明:

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

余额充值