【发布时间】:2019-06-23 00:16:48
【问题描述】:
这是我的异常处理程序
@ControllerAdvice(basePackageClasses = [SignUpController::class])
class ControllerAdvice: ResponseEntityExceptionHandler() {
@ExceptionHandler(Exception::class)
@ResponseBody
fun handleControllerException(request: HttpServletRequest, ex: Throwable): ResponseEntity<*> {
val status = HttpStatus.CONFLICT
return ResponseEntity<Any>(ApiError(status.value(), ex.message), status)
}
}
还有我的自定义类
data class ApiError(val status: Int, val message: String?)
处理程序正常工作,但抛出错误,如下所示
{
"timestamp": "2019-01-29T19:17:22.541+0000",
"status": 401,
"error": "Unauthorized",
"message": "could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement",
"path": "/sign-up"
}
但我应该关注类似的东西
{
"apierror": {
"status": ...,
"message": ..."
}
}
我是本教程的基础
https://www.toptal.com/java/spring-boot-rest-api-error-handling https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-error-handling https://medium.com/@jovannypcg/understanding-springs-controlleradvice-cd96a364033f
我做错了什么?我缺少任何配置?
【问题讨论】:
标签: spring spring-boot kotlin error-handling exception-handling