spring-boot - 创建自定义 ErrorWebExceptionHandler 失败
问题描述
我试图ErrorWebExceptionHandler
通过扩展默认值在 Spring Boot 2 中创建自己的,但我的应用程序无法以以下消息启动:
Caused by: java.lang.IllegalArgumentException: Property 'messageWriters' is required
at org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler.afterPropertiesSet(AbstractErrorWebExceptionHandler.java:214) ~[spring-boot-autoconfigure-2.0.4.RELEASE.jar:2.0.4.RELEASE]
我的处理程序(Kotlin 代码):
@Component
@Order(-2)
class SampleErrorWebExceptionHandler(
errorAttributes: ErrorAttributes?,
resourceProperties: ResourceProperties?,
errorProperties: ErrorProperties?,
applicationContext: ApplicationContext?
) : DefaultErrorWebExceptionHandler(errorAttributes, resourceProperties, errorProperties, applicationContext) {
override fun logError(request: ServerRequest, errorStatus: HttpStatus) {
// do something
}
}
可能是什么原因?
解决方案
请尝试在构造函数中添加 ServerCodecConfigurer 的依赖项
GlobalErrorWebExceptionHandler(
ErrorAttributes errorAttributes,
ResourceProperties resourceProperties,
ApplicationContext applicationContext,
ServerCodecConfigurer configurer
) {
super(errorAttributes, resourceProperties, applicationContext);
this.setMessageWriters(configurer.getWriters());
}
推荐阅读
- terminal - Tmux 无法退出
- java - Minimax 算法逻辑输出意外结果
- swift - deleteSections 只删除最后一个部分
- javascript - 将 HTML 变量转换为 PHP 变量
- javascript - Express js bodyparser post req.body 不起作用?
- java - 按key的值排序List<HashMap<String,Object>>
- javascript - 在反应中将 gif 上传到 giphy api
- python - 泡菜文件是否会因版本而异?
- javascript - Summernote 下拉菜单中的动态内容
- php - 无法限制上下菜单上的数字输入以消除重复项