1. 程式人生 > 其它 >輸入時間,找出3小時後的工作時間點,如果遇到休息時間,需要跳過(就是休息時間不計時),輸入時間格式:2020-03-21 14:23:31

輸入時間,找出3小時後的工作時間點,如果遇到休息時間,需要跳過(就是休息時間不計時),輸入時間格式:2020-03-21 14:23:31

技術標籤:swagger2java

SpringBoot整合swagger

  1. 新建一個springboot 的web專案

  2. 匯入相關依賴

    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.9.2</version>
    </dependency>
    
    <dependency>
      <groupId>io.springfox</
    groupId
    >
    <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
  3. 編寫一個Hello工程

    @RestController
    public class HelloController {
    
        @RequestMapping("/hello")
        public String Hello(){
            return "hello";
        }
    }
  4. 配置swagger 的config檔案

    @Configuration
    
    @EnableSwagger2 //開啟swagger2
    public class SwaggerConfig {
    }
    
  5. 測試執行

    訪問http://localhost:8080/swagger-ui.html

    螢幕快照 2020-11-26 下午6.10.40

配置Swagger

swagger的bean例項:docket

@EnableSwagger2 //開啟swagger2
public class SwaggerConfig {

    //配置了Swagger的Docket的bean例項
    @Bean
    public Docket docket(){
        return
new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()); } private ApiInfo apiInfo(){ //作者資訊 Contact contact = new Contact("lu","https://www.bilibili.com/video/BV1Y441197Lw?p=2","[email protected]"); return new ApiInfo("lu的swagger文件", "la vie en rose", "v1.0","https://www.bilibili.com/video/BV1Y441197Lw?p=2" ,contact,"Apache 2.0","http://www.apache.org/licenses/LICENSE-2.0",new ArrayList()); }
螢幕快照 2020-11-26 下午6.28.18

swagger配置掃描介面

docket.Select

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

Select.apis.paths.build

enable: 是否啟動swagger, 如果為false,則swagger不能再瀏覽器中訪問

public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(
                apiInfo()).enable(false).select().
                apis(RequestHandlerSelectors.basePackage("com.lu.swaggerdemo.controller")).build();//工廠模式
    }

希望swagger在生產環境中使用,在釋出的時候不使用:

  • 首先判斷是不是生產環境
  • 注入enable()
@Bean
    public Docket docket(Environment environment){
      //--------------------------------------  
      //設定要顯示的swagger環境
        Profiles profiles = Profiles.of("dev","test");
        // 通過environment.acceptsProfiles判斷是否處在自己設定的環境當中
        boolean flag = environment.acceptsProfiles(profiles);
       //-------------------------------------------------

        return new Docket(DocumentationType.SWAGGER_2).apiInfo(
                apiInfo()).enable(flag).select().
                apis(RequestHandlerSelectors.basePackage("com.lu.swaggerdemo.controller")).build();//工廠模式
    }

配置API文件分組:.groupName("")

@Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(
                apiInfo()).groupName("lu").enable(true).select().
                apis(RequestHandlerSelectors.basePackage("com.lu.swaggerdemo.controller")).build();//工廠模式
    }

如何配置多個分組:

建立多個docket例項:

		@Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");

    }

    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");

    }
    //配置了Swagger的Docket的bean例項
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(
                apiInfo()).groupName("lu").enable(true).select().
                apis(RequestHandlerSelectors.basePackage("com.lu.swaggerdemo.controller")).build();//工廠模式
    }
   

介面註釋

如何顯示實體類

介面返回值存在實體類就會被掃描

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String Hello(){
        return "hello";

    }

    // 只要介面返回值存在實體類就會被掃描
    @GetMapping("/user")
    public User user(){
        return new User();
    }
}

給實體類➕註釋

@ApiModel("…")

@ApiModelProperty("…")

@ApiModel("使用者實體類")
public class User {
    @ApiModelProperty("使用者名稱")
    public String userName;
    @ApiModelProperty("密碼")
    public String pwd;

}
螢幕快照 2020-11-27 下午5.23.43