配置Swagger和註解的使用

配置完成後,下面有註解的使用

添加依賴

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

配置Swagger swaggerconfig

放到config裏面
在這裏插入圖片描述

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2 
public class SwaggerConfig {
    //配置了Swagger的Docket的bean實例
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_12)
                .apiInfo(apiInfo())
                .enable(true)//enable是否啓用swagger ,如果爲false , 則swagger不能再瀏覽器中訪問
                .select()
                //RequestHandlerSelectors , 配置掃描接口的方式
                //basepackage:指定要掃描的包
                //any():全部掃描
                //none():不掃描
                //withClassAnnotation():掃描類上的註解,參數是一個註解的反射對象
                //withMethodAnnotation():掃描方法上的註解
                .apis(RequestHandlerSelectors.any())
                //paths():過濾什麼路徑
                //.paths(PathSelectors.ant("msfh/**"))
                .build()
                ;
    }

    //配置swagger的信息 = apiinfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("致橡樹丶", "https://blog.csdn.net/qq_45777315", "[email protected]");
        return  new ApiInfo(
                "zyh的Swagger API文檔",
                "好好學習 天天向上",
                "1.0",
                "https://blog.csdn.net/qq_45777315",
                 contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                 new ArrayList<VendorExtension>());

    }
}

在本地測試

localhost:8080/swagger-ui.html

註解的使用

這裏是詳細的使用,我是改簡單概括下
https://blog.csdn.net/Booleaning/article/details/88829142

在方法裏的註解

//controller的一個類上,標誌這個類是swagger資源
@Api(value = "說明幹啥的")

//作用於方法之上,用戶表示一個http的請求
@ApiOperation(value = "方法描述", notes = "方法詳情描述")

//用於參數字段的說明
@ApiParam(name = "參數名". value= "參數說明", required = "是否必須")
```在這裏插入圖片描述
![在這裏插入圖片描述](https://img-blog.csdnimg.cn/20200615103316686.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1Nzc3MzE1,size_16,color_FFFFFF,t_70)

## 在實體裏面的註解

```java
//作用於類,對類進行說明,用於實體類接收或者返回
@ApiModel(value = "對象名", description = "對象描述")

//作用於實體類,用於實體類中某個字段
@ApiModelProperty(value = "參數解釋",name = "參數名",required = "是否必填")

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章