1. 程式人生 > 其它 >Swagger簡單使用(個人用)

Swagger簡單使用(個人用)

技術標籤:Springbootspringspring boot

用於前後端的分離,可以實時的展示後端的介面

一般Springboot配置,都會有一個config類,用於書寫配置(config配置)
SwaggerConfig

package com.mystudy.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.
web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; @Configuration //開啟Swagger2 @EnableSwagger2 public class SwaggerConfig { //分組問題 @Bean public Docket docket1(){ return new Docket((DocumentationType.SWAGGER_2)).groupName
("A"); } @Bean public Docket docket2(){ return new Docket((DocumentationType.SWAGGER_2)).groupName("B"); } @Bean public Docket docket3(){ return new Docket((DocumentationType.SWAGGER_2)).groupName("C"); } //配置Swagger的Docket的bean例項 @Bean //設定開發時不開啟swagger,而生產時開啟swagger public Docket docket(Environment environment){ //設定要顯示的Swagger環境 Profiles profiles = Profiles.of("dev","test"); //獲取當前專案的環境,通過environment.acceptsProfiles(profiles)判斷是否處在自己設定的環境當中 boolean flag = environment.acceptsProfiles(profiles); //1.配置swagger資訊 return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) //enable()是否啟用swagger,如果為false,則Swagger不能在瀏覽器中訪問(用於開發環境和生產環境是否開啟swagger) //.enable(flag) //設定分組的名字 .groupName("myStudy") .select() //requestHandler->表示的為requestHandler的介面,要寫實現類 或者自定義實現類 //則這裡使用它的RequestHandlerSelectors實現類 //RequestHandlerSelectors:配置要掃描介面的方式 //basePackage():掃描指定目錄下的包 //any():掃描全部的包 //none():都不掃描 //withClassAnnotation():掃描類上的註解,引數是一個註解的反射物件 //withMethodAnnotation(): 掃描方法上的註解 .apis(RequestHandlerSelectors.basePackage("com.mystudy.controller")) //過濾路徑(只掃描請求中帶有mystudy下的請求的方法) //.paths(PathSelectors.ant("/mystudy/**")) .build(); //build } //配置Swagger資訊(APIInfo) private ApiInfo apiInfo(){ //作者資訊 Contact contact = new Contact("朱潤澤", "http://localhost:8080", "[email protected]"); return new ApiInfo("MyStudy Swagger介面日誌", "三年不飛一飛沖天,三年不鳴一鳴驚人", "v1.0", "http://localhost:8080", contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList()); } }

實體類的書寫(要加Swagger上面的註解)

//@ApiModel()用於Swagger展示時的文件註釋
@ApiModel("使用者實體類")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    //@ApiModelProperty("使用者名稱")給生成的文件加入註釋
    @ApiModelProperty("使用者名稱")
    private String username;

    @ApiModelProperty("密碼")
    private String password;
}

Controller上的書寫

@RestController
public class HelloController {

    //還有一個請求時error
    @GetMapping("/hello")
    public String hello(){
        return "hello,Spring";
    }

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

    @ApiOperation("Hello控制類")
    @GetMapping("/hello2")
    public String hello2(@ApiParam("使用者名稱") String username){
        return "hello"+username;
    }

    @ApiOperation("Post測試")
    @GetMapping("/postTest")
    public User postTest(@ApiParam("使用者") User user){
        return user;
    }
}