【发布时间】:2020-12-09 13:33:18
【问题描述】:
您好 Stackoverflow 专家,
如何处理 org.hibernate.exception.ConstraintViolationException(以及所有其他休眠异常)和 RuntimeException 以将以下 json 返回到前端?
{
"timestamp": "2020-08-18T21:03:36.174Z",
"code": "CODE_20500",
"status": 500,
"details": "An internal error occurred."
}
实际上我已经声明了我的 ControllerDevice 并按照 https://www.baeldung.com/problem-spring-web 的 Baeldung 指南的步骤进行操作:
@ControllerAdvice
public class MyErrorHandlingControllerAdvice implements ProblemHandling {
}
我还在“application.properties”文件中设置了这些属性
spring.resources.add-mappings=false
spring.mvc.throw-exception-if-no-handler-found=true
servlet.http.encoding.force=true
但是,当尝试将重复的主键保存在数据库中时,我收到以下消息:
{
"timestamp": "2020-08-20T08:44:11.342+02:00",
"status": 400,
"detail": "could not execute statement; SQL [n/a]; constraint [uq_mykatalog_name]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement"
}
出于安全原因,我们不能将数据库字段公开。
我正在使用这些库版本:
springboot 版本 = 2.3.3.RELEASE
实现“org.zalando:problem-spring-web:0.23.0”
请帮忙! 谢谢。
【问题讨论】:
标签: java spring-boot exception spring-data-jpa