知道了swagger2的好,但要怎样的好好利用,才能好上加好?
1,用swagger2注解代替掉字段注释吧
为什么这样说,字段注释是我们写来解释字段含义的
public class PageVO {
/**
* 第几页
*/
private Integer current;
/**
* 每页条数
*/
private Integer size;
}
这样写,没毛病,但是就只能我们自己看。
@ApiModel(description = "分页入参模型")
public class PageVO {
@ApiModelProperty(value = "第几页")
private Integer current;
@ApiModelProperty(value = "每页条数")
private Integer size;
}
而这样写,不仅我们自己能看,swagger2还能依据这些注解生成出文档给别人看,就不用再写接口文档了。
2,把注解写全,当做接口文档来写
@ApiModel(description = "分页入参模型")
public class PageVO {
@ApiModelProperty(value = "第几页",required = true,example = "1")
private Integer current;
@ApiModelProperty(value = "每页条数",required = true,example = "10")
private Integer size;
}
不用多说,写的越全,自己看的明白,别人也更明白。
3,换一个更好看的swagger-ui
个人觉得自带的swagger-ui很丑,有人也这么觉得,所以有人改进了swagger界面。
怎么换呢?
<!--https://doc.xiaominfo.com/knife4j/-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
引入这个依赖,将
http://localhost:20001/swagger-ui.html
改成
http://localhost:20001/doc.html
o了