Java Kotlin-弹簧座-控制器建议,例外情况Handler赢得';不能被调用
为了简化错误处理,我需要一个ExceptionHandler,我使用了4。点上 我的异常处理程序类如下所示:Java Kotlin-弹簧座-控制器建议,例外情况Handler赢得';不能被调用,java,spring,rest,exception,kotlin,Java,Spring,Rest,Exception,Kotlin,为了简化错误处理,我需要一个ExceptionHandler,我使用了4。点上 我的异常处理程序类如下所示: @ControllerAdvice class APIExceptionHandler : ResponseEntityExceptionHandler() { @ExceptionHandler(value = [(TestException::class)]) fun handleConflict(exception: TestException, request:
@ControllerAdvice
class APIExceptionHandler : ResponseEntityExceptionHandler() {
@ExceptionHandler(value = [(TestException::class)])
fun handleConflict(exception: TestException, request: WebRequest): ResponseEntity<Any> {
println("Handle")
return handleExceptionInternal(exception, "Response Body", HttpHeaders(), HttpStatus.BAD_REQUEST, request)
}
}
无论如何,在我的RestController
中,我只是在发出任何调用后立即抛出一个异常:
@GetMapping("/lobby/close")
fun closeLobby(@RequestParam(value = "uuid") uuid: String, @RequestHeader(value = "userSession") userSession: String): ResponseEntity<Any> {
throw TestException()
}
@GetMapping(“/lobble/close”)
fun CloseLobble(@RequestParam(value=“uuid”)uuid:String,@RequestHeader(value=“userSession”)userSession:String):ResponseEntity{
抛出TestException()
}
但不会调用异常处理程序
然而,称之为:
@GetMapping("/lobby/error")
fun error(): ResponseEntity<Any> {
throw TestException()
}
@GetMapping(“/lobble/error”)
有趣的错误():ResponseEntity{
抛出TestException()
}
它被调用
我不太明白区别是什么,除了第一个版本需要参数和特定的头
2018年3月24日更新
问题似乎是,如果客户端请求的格式不正确,则不会调用ExceptionHandler
默认情况下,格式错误的请求会导致非常详细的错误报告,但自定义异常处理程序似乎会禁用此功能。现在我有了一个解决方案,尽管我认为这不应该是理想的方式 因为我的控制器没有基类,所以我没有创建一个包含处理异常的函数的
BaseController
,这些函数只是用ExceptionHandler
注释,它们的方法列表包含它们应该处理的异常
由于BasicController
类不扩展任何其他处理异常的Spring类,因此它不会覆盖处理错误请求等问题的默认行为
我很乐意接受任何更好的解决方案,因为我认为这不应该是理想的方式。我让它工作了,这是我的代码
@ControllerAdvice
class ControllerAdviceRequestError : ResponseEntityExceptionHandler() {
@ExceptionHandler(value = [(UserAlreadyExistsException::class)])
fun handleUserAlreadyExists(ex: UserAlreadyExistsException,request: WebRequest): ResponseEntity<ErrorsDetails> {
val errorDetails = ErrorsDetails(Date(),
"Validation Failed",
ex.message!!
)
return ResponseEntity(errorDetails, HttpStatus.BAD_REQUEST)
}
}
数据类
data class ErrorsDetails(val time: Date, val message: String, val details: String)
MyController:
@PostMapping(value = ["/register"])
fun register(@Valid @RequestBody user: User): User {
if (userService.exists(user.username)) {
throw UserAlreadyExistsException("User with username ${user.username} already exists")
}
return userService.create(user)
}
当您调用closeLobby()
时,您在日志中看到异常了吗?没有,我没有看到任何异常。您得到了什么http响应?404?我得到400的回复。但是,这并不是我的例外,因为我得到了400,即使我在那里改变它。听起来你只是没有正确的提出请求。如果删除异常并返回正确的值,会发生什么情况?我尝试了这个方法,只有在必须处理特定异常时才有效。只要我有两个方法,一个用于用户定义的异常,一个用于一般异常,它就会失败。每当抛出用户定义的异常时,它都会被用@ExceptionHandler(exception::class)
注释的方法捕获,而不是用@ExceptionHandler(useralreadyexistexception::class)
注释的方法捕获。有什么解决办法吗?当我在java中尝试它时,它就像一个符咒,但在kotlin中却没有。我不知道是否有任何与kotin相关的特定错误。在这种情况下,请尝试扩大搜索范围
data class ErrorsDetails(val time: Date, val message: String, val details: String)
@PostMapping(value = ["/register"])
fun register(@Valid @RequestBody user: User): User {
if (userService.exists(user.username)) {
throw UserAlreadyExistsException("User with username ${user.username} already exists")
}
return userService.create(user)
}