【问题标题】:@ControllerAdvice not working Spring 5.1.6@ControllerAdvice 不工作 Spring 5.1.6
【发布时间】:2019-09-19 13:41:18
【问题描述】:

我正在尝试使用 Kotlin 在 Spring 中实现错误处理程序,但应用程序似乎无法识别它:我总是得到 /error 页面并且未处理异常。

@EnableWebMvc,在其他类似问题中建议,对我不起作用。

这是我的实际代码:

@ControllerAdvice
class UserExceptionHandler {

     @ExceptionHandler(ConstraintViolationException::class)
     fun methodArgumentTypeMismatchException(e: ConstraintViolationException): ResponseEntity<*> {
         return ResponseEntity
            .status(HttpStatus.FORBIDDEN)
            .body("Constraints Involved. Pay Attention To The Parameters")
     }
}

以下是我的@RestController

@RestController
@RequestMapping("/api")
class UserController (@Autowired private val userRepository: UserRepository) {

     @PostMapping("/new-user")
     fun createNewUser(@RequestParam mailAddress: String,
                       @RequestParam password: String): ResponseEntity<UserEntity> =
         ResponseEntity.ok(userRepository.saveAndFlush(UserEntity(mailAddress = mailAddress, password = password)))
}

UserEntity 在邮件不是唯一的情况下抛出异常:

@Entity
data class UserEntity(
        @Id @NotBlank @GeneratedValue(strategy = GenerationType.IDENTITY)
        val id: Long? = null,

        @Column(unique=true) @NotBlank
        val mailAddress: String,

        @NotBlank
        var password: String
)

我做错了什么?我展示的文件共享同一个包。

编辑:以下是 Spring LOG 上的堆栈跟踪

ERROR 32916 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["UK_KQE6HHKTR4W4E02H0KN61F442_INDEX_F ON PUBLIC.USER_ENTITY(MAIL_ADDRESS) VALUES ('asd@lol.it', 97)"; SQL statement:
insert into user_entity (id, mail_address, password) values (null, ?, ?) [23505-197]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause

WARN 33436 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Failure in @ExceptionHandler public org.springframework.http.ResponseEntity<?> unito.taas.project.user.UserExceptionHandler.methodArgumentTypeMismatchException(javax.validation.ConstraintViolationException)

java.lang.IllegalStateException: Could not resolve parameter [0] in public org.springframework.http.ResponseEntity<?> unito.taas.project.user.UserExceptionHandler.methodArgumentTypeMismatchException(javax.validation.ConstraintViolationException): No suitable resolver

【问题讨论】:

  • 您能否确认您确实验证了参数并且ConstraintViolationException 被抛出在无效对象上?当这种情况发生时,你能提供堆栈跟踪吗?
  • @PiotrPodraza 我不确定我是否理解了这个问题,但我可以告诉你,ConstraintViolationException(当然是故意的)因为重复的密钥而被抛出。我的问题是ControllerAdvice 无法处理它,我不知道为什么。

标签: spring spring-boot spring-mvc error-handling controller-advice


【解决方案1】:

您从错误的包导入 ConstraintViolationException - javax.validation 而不是 org.hibernate.exception

确保在UserExceptionHandler 中使用org.hibernate.exception.ConstraintViolationException

【讨论】:

    猜你喜欢
    • 2013-08-23
    • 2021-05-22
    • 2017-12-17
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 2020-06-05
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多