1. 程式人生 > 實用技巧 >push to origin/master was rejected錯誤解決方案

push to origin/master was rejected錯誤解決方案

Swagger

1、Springboot整合Swagger

1、先建立一個springboot專案

2、先匯入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>

3、編寫swaggerconfig配置

@Configuration
@EnableSwagger2
public class SwaggerConfig {
}

4、這樣就可以在localhost:8080/swagger-ui.html頁面看到相應的api介面

2、配置Swagger資訊

配置swagger資訊,需要配置Docker的bean資訊。

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
    }
    private ApiInfo apiInfo(){
        Contact contact = new Contact("錦豪", "https://www.cnblogs.com/xiaopanjava/", "[email protected]");
        return new ApiInfo(
                "豪哥的apI文件",
                "小胖學Swagger",
                "v2.0",
                "https://www.cnblogs.com/xiaopanjava/",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<VendorExtension>());
    }
}

效果如下:

3、配置Swagger介面資訊

  @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //RequestHandlerSelectors 配置要掃描介面的方式
                //basePackage 指定要掃描的包
                //any :掃描全部
                //none :不掃描
                //withClassAnnotation 掃描類上的註解,引數是一個註解反射物件
                //withMethodAnnotation  掃描方法上的註解
                .apis(RequestHandlerSelectors.basePackage("com.example.gitdemo.controller"))
                //過濾需要掃描的路勁
                .paths(PathSelectors.ant("/controller/*"))
                .build();
    }

根據這些可以配置掃描介面的資訊

4、配置Swagger分組資訊

  .groupName("小胖")

同樣的可以設定多個分組。只需要編寫多個docket例項了

 @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("錦豪");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("豪哥");
    }

5、配置實體類

 //只要我們的介面中,返回值存在實體類,它就會被掃描到Swagger中
 @PostMapping("/user")
public User user(){
     return new User();
 }

其中,還可以使用註解來解釋實體類中的資訊

package com.example.gitdemo.pojo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("使用者實體類") //解釋實體類的資訊
public class User {
    @ApiModelProperty("使用者名稱")  //解釋實體類屬性資訊
    public String name;
    @ApiModelProperty("年齡")
    public int age;
}