Migrating from SpringFox - Springdoc-openapi:
Remove springfox and swagger 2 dependencies. Add springdoc-openapi-ui dependency instead.
Before:
implementation group: 'io.springfox', name: 'springfox-swagger2', version: '.'2.9.2''
implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
After:
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.8'
Before:
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
.genericModelSubstitutes(Optional.class)
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.any())
.build()
.securitySchemes(apiKeyList());
}
private List<ApiKey> apiKeyList() {
return
newArrayList(
new ApiKey("Authorization", "Authorization","header"),
new ApiKey("ServiceAuthorization", "ServiceAuthorization", "header")
);
After:
@Bean
public GroupedOpenApi publicApi(OperationCustomizer customGlobalHeaders) {
return GroupedOpenApi.builder()
.group("rd-location-ref-api")
.pathsToMatch("/**")
.build();
}
@Bean
public OperationCustomizer customGlobalHeaders() {
return (Operation customOperation, HandlerMethod handlerMethod) -> {
Parameter serviceAuthorizationHeader = new Parameter()
.in(ParameterIn.HEADER.toString())
.schema(new StringSchema())
.name("ServiceAuthorization")
.description("Keyword `Bearer` followed by a service-to-service token for a whitelisted micro-service")
.required(true);
Parameter authorizationHeader = new Parameter()
.in(ParameterIn.HEADER.toString())
.schema(new StringSchema())
.name("Authorization")
.description("Authorization token")
.required(true);
customOperation.addParametersItem(authorizationHeader);
customOperation.addParametersItem(serviceAuthorizationHeader);
return customOperation;
};
}
- Replace swagger 2 annotations with swagger 3 annotations
- Package for swagger 3 annotations is io.swagger.v3.oas.annotations.
- @Api → @Tag
- @ApiIgnore → @Parameter(hidden = true) or @Operation(hidden = true) or @Hidden
- @ApiImplicitParam → @Parameter
- @ApiImplicitParams → @Parameters
- @ApiModel → @Schema
- @ApiModelProperty(hidden = true) → @Schema(accessMode = READ_ONLY)
- @ApiModelProperty → @Schema
- @ApiOperation(value = "foo", notes = "bar") → @Operation(summary = "foo", description = "bar")
- @ApiParam → @Parameter
- @ApiResponse(code = 404, message = "foo") → @ApiResponse(responseCode = "404", description = "foo")
- If you’re using an object to capture multiple request query params, annotation that method argument with @ParameterObject
- This step is optional: Only if you have multiple Docket beans replace them with GroupedOpenApi beans.
No comments:
Post a Comment