天天看點

Swagger2如何使用更好?1,用swagger2注解代替掉字段注釋吧2,把注解寫全,當做接口文檔來寫3,換一個更好看的swagger-ui

知道了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界面。

Swagger2如何使用更好?1,用swagger2注解代替掉字段注釋吧2,把注解寫全,當做接口文檔來寫3,換一個更好看的swagger-ui
Swagger2如何使用更好?1,用swagger2注解代替掉字段注釋吧2,把注解寫全,當做接口文檔來寫3,換一個更好看的swagger-ui

怎麼換呢?

<!--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了