OpenAPI SpringBoot 3,如何添加全局错误响应代码

axkjgtzd  于 12个月前  发布在  Spring
关注(0)|答案(1)|浏览(219)

我试图在Sping Boot 3项目中配置Swagger,我想全局设置HTTP状态码响应,而不是在每个端点上复制粘贴代码。我记得我们曾经在Swagger 2或Docket中这样做过,但现在事情被转换为OpenAPI,我找不到这样做的方法。
可以有人请帮助我修改下面的代码,以便我添加全球响应。

@Configuration
public class OpenApiConfig {

    @Bean
    public OpenAPI usersMicroserviceOpenAPI() {

        Server localServer = new Server()
                .url("http://localhost:8080")
                .description("A Server URL");

        Contact contact = new Contact()
                .email("[email protected]")
                .name("Exampl man");

        Info info = new Info()
                .contact(contact)
                .description("Spring Boot 3 + Open API 3")
                .summary("Demo of Spring Boot 3 & Open API 3 Integration")
                .title("Sample")
                .version("V1.0.0")
                .license(new License().name("Apache 2.0").url("http://springdoc.org"));

        return new OpenAPI().info(info).
                addServersItem(localServer);
    }
}

字符串
我不想在每个API上面写以下内容

@ApiResponses(value = {
            @ApiResponse(responseCode = "400", description = "Bad Request",
                    content = { @Content(mediaType = "application/json",
                            schema = @Schema(implementation = ErrorRes.class))}),
            @ApiResponse(responseCode = "404", description = "Resource Not Found",
                    content = { @Content(mediaType = "application/json",
                            schema = @Schema(implementation = ErrorRes.class))})
    })


我试着创建一个接口,它有上面的注解,并在每个REST控制器上实现,它确实很神奇,但它删除了默认的200响应,并根据方法的签名返回了Class对象。

3pmvbmvn

3pmvbmvn1#

您可以将创建OpenApiCustomizer bean的方法添加到OpenApiConfig类中,如下所示。

@Bean
  public OpenApiCustomizer openApiCustomizer() {
    ResolvedSchema errResSchema =
        ModelConverters.getInstance().resolveAsResolvedSchema(new AnnotatedType(ErrorRes.class));
    Content content =
        new Content().addMediaType("application/json", new MediaType().schema(errResSchema.schema));
    return openApi ->
        openApi
            .getPaths()
            .values()
            .forEach(
                pathItem ->
                    pathItem
                        .readOperations()
                        .forEach(
                            operation ->
                                operation
                                    .getResponses()
                                    .addApiResponse(
                                        "400",
                                        new ApiResponse()
                                            .description("Bad Request")
                                            .content(content))
                                    .addApiResponse(
                                        "404",
                                        new ApiResponse()
                                            .description("Resource Not Found")
                                            .content(content))));
  }

字符串
它将把API响应附加到应用程序中的所有路径。

相关问题