相关疑难解决方法(0)

如何使用 ExceptionHandler 在 Spring Boot Rest 中包装 Path Not Found 异常?

我正在使用 Spring Boot 和 Spring Rest 示例。在此示例中,我传递自定义标头,如果该值有效,则端点被成功调用,如果自定义标头值不正确,则我得到以下响应,我想将其包装到使用 ExceptionHandler 将其显示给最终用户@ControllerAdvice

注意:我浏览了Spring mvc - 如何将所有错误的请求映射到单个方法,但在我的例子中,我根据 CustomHeader 做出决定。

{
    "timestamp": "2020-01-28T13:47:16.201+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/employee-data/employee-codes"
}
Run Code Online (Sandbox Code Playgroud)

控制器

@Operation(summary = "Find Employee")
@ApiResponses(value = { @ApiResponse(code = 200, message = "SUCCESS"),
        @ApiResponse(code = 500, message = "Internal Server Error") })
@Parameter(in = ParameterIn.HEADER, description = "X-Accept-Version", name = "X-Accept-Version", 
        content = @Content(schema = @Schema(type = "string", defaultValue = "v1", 
        allowableValues = {HeaderConst.V1}, implementation …
Run Code Online (Sandbox Code Playgroud)

spring spring-boot spring-rest

4
推荐指数
1
解决办法
8243
查看次数

标签 统计

spring ×1

spring-boot ×1

spring-rest ×1