集成Swagger
原来需要用postman来测试接口,现在我们使用Swagger2
(一)导入两个依赖
<!-- 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>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
(二)配置Swagger2
1.写一个config配置包
2.在SwaggerConfig中配置
package com.gy.springcloud.config;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration //表明这是一个配置类
@EnableSwagger2 //开启Swagger
public class SwaggerConfig {
}
其实配置两个注解我们就可以简单的使用了
(三)访问
端口号+swagger-ui.html 就可以访问
(四)更复杂的配置Swagger
1.在返回的类中使用注解给实体类进行注释
package com.gy.springcloud.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
//这个类是用来传给前端的
@ApiModel("实体类")
public class CommonResult <T>{
private Integer code;
@ApiModelProperty("异常信息")
private String massage;
@ApiModelProperty("数据")
private T data;
@ApiModel("")里面写类的注释信息
@ApiModelProperty(“”)里面写属性的注释信息
(五)其实不复杂配置也行,直接用
1.找到你的接口点开
2.点击你想要检验的请求
3.点这里
4.输入数据,提交
5.查看结果
6.查看数据库结果
当然这里的ID有点问题是因为我设置id自动加的