先說結論:
????????Swgger 3.0? 與Swagger 2.0? 區別很大,Swagger3.0用了最新的注釋實現更強大的功能,同時使得代碼更優雅。
? ? ? ? 就個人而言,如果新項目推薦使用Swgger 3.0,對于工具而言新的一定比舊的好;對接于舊項目原有Swagger 2.0版本不變就不要變,因為它作為輔助功能能達到你的需求就可以了(當然我一再聲明這只代表我的個人看法,歡迎留言討論)。
一、Maven配置方面差異
Swagger 2.0
<!-- swagger -->
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.8.0</version>
</dependency>
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.8.0</version>
</dependency>
Swagger 3.0
<dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.3.0</version>
</dependency>
配置application.yml? 或者application.properties
application.yml
spring:mvc:pathmatch:matching-strategy: ant_path_matcher
application.properties
spring.mvc.pathmatch.matching-strategy= ant_path_matcher
二、配置類區分
Swagger 2.0
@Configuration
@EnableSwagger2
public class SwaggerConfig {@Beanpublic Docket createRestApi(){// 添加請求參數,我們這里把token作為請求頭部參數傳入后端ParameterBuilder parameterBuilder = new ParameterBuilder();
// List<Parameter> parameters = new ArrayList<Parameter>();
// parameterBuilder.name("token").description("令牌")
// .modelRef(new ModelRef("string")).parameterType("header").required(false).build();
// parameters.add(parameterBuilder.build());
// return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
// .apis(RequestHandlerSelectors.any()).paths(PathSelectors.any())
// .build().globalOperationParameters(parameters);return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build();}private ApiInfo apiInfo(){
// return new ApiInfoBuilder()
// .title("Kitty API Doc")
// .description("This is a restful api document of Kitty.")
// .version("1.0")
// .build();return new ApiInfoBuilder().build();}}
Swagger 3.0
@Configuration
public class OpenAPIConfig {/*** 這個方法可以不配置會自動去掃描,但配置了更好,因為掃描有了目標性會更快* 這個方法是創建分組* @return*/@Beanpublic GroupedOpenApi publicApi() {String[] paths = {"/**"};String[] packages = {"com.example.test.controller"};//掃描的路徑return GroupedOpenApi.builder().group("public").pathsToMatch(paths).packagesToScan(packages).build();}@Beanpublic OpenAPI openAPI() {return new OpenAPI().info(new Info().title("接口文檔標題").description("SpringBoot3 集成 Swagger3接口文檔").version("v1")).externalDocs(new ExternalDocumentation().description("項目API文檔").url("/"));}
}
三、常注解差異
注解位置 | Swagger 2.0 | Swagger 3.0? |
---|---|---|
Controller 類 | @Api | @Tag(name="接口名",description="接口描述") |
Controller 方法 | @ApiOperation | @Operation(summary =“接口方法描述”) |
@ApilmplicitParams | @Parameters | |
Controller 方法上 @Parameters 里 | @ApiImplicitParam | @Parameter(description=“參數描述”) |
Controller 方法的參數上 | @ApiParam | @Parameter(description=“參數描述”) |
@ApiIgnore | @Parameter(hidden = true)?或?@Operation(hidden = true)?或?@Hidden | |
DTO類上 | @ApiModel | @Schema |
DTO屬性上 | @ApiModelProperty |
Swagger 2
? ? ? ? controller代碼
? ? ? ? DTO
?
Swagger 3
????????Controller代碼
@RestController
@Tag(name = "TestController",description = "測試接口")
@RequestMapping(value = "/swaggertest")
public class TestController {@Operation(summary = "測試接口",description = "測試接口")@GetMapping(value = "/noHiddenApi")public String noHiddenApi(@Parameter(name = "id",description = "這個ID代表.......") Integer id){return "noHiddenApi";}
}
? ? ? ? DTO代碼
@Schema(description = "用戶實體類")
public class SysUser {@Schema(description = "用戶id")private Integer id;@Schema(description = "用戶名")private String username;@Schema(description = "密碼")private String password;}
后記
花了近一個小時的時間寫這個文章,如果有問題請留言指正,確對您有幫助請點贊收藏,謝謝觀看。