Java Can';无法获取PSQLException的错误消息

Java Can';无法获取PSQLException的错误消息,java,postgresql,spring-boot,exception-handling,spring-data-jpa,Java,Postgresql,Spring Boot,Exception Handling,Spring Data Jpa,我开发了一个API服务,可以向数据库添加一些数据。我为实体的名称字段设置了唯一约束。每当我尝试插入重复的name值时,我都会给出以下堆栈跟踪 Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "uk_tten_courseservice_program_table_program_name" Detail: Key (program_name)

我开发了一个API服务,可以向数据库添加一些数据。我为实体的
名称
字段设置了唯一约束。每当我尝试插入重复的
name
值时,我都会给出以下堆栈跟踪

Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "uk_tten_courseservice_program_table_program_name"
  Detail: Key (program_name)=(jkjkncj) already exists.
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:334)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
    ... 119 more
这是一个嵌套异常,我希望从中将错误消息发送到前端。根异常是:

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [uk_tten_courseservice_program_table_program_name]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
我在实体类中添加的内容如下:

@Entity
@Table(name = "tten_courseservice_program_table", uniqueConstraints = {
                        @UniqueConstraint(name = "uk_tten_courseservice_program_table_program_name", columnNames = { "program_name" }) })
public class Program {

    @Column(name = "program_name")
    private String programName;
    //other attributes and getter setters. 
}
那么我在这里错过了什么?我试着放置:

catch(PSQLexception e)
但是它没有捕获我的错误,因为它不是根异常。而
DataIntegrityException
有非常模糊的信息,如:

could not execute statement; SQL [n/a]; constraint [uk_tten_courseservice_program_table_program_name]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

有没有其他方法来实现这些目标?我知道的一种方法是在保存每一条记录之前对数据库运行搜索查询,以检查它是否已经存在,但我认为这样做效率不高。提前谢谢

要处理此类异常,您可以实现。要获取异常的根本原因,您可以使用实用程序方法
org.springframework.core.NestedExceptionUtils#getmostsspecificcause

例如:

@ControllerAdvice
public class ExceptionHandler {

    @ExceptionHandler(DataIntegrityViolationException.class)
    public ResponseEntity<?> conflict(DataIntegrityViolationException e) {

        String message = NestedExceptionUtils.getMostSpecificCause(e).getMessage();
        ErrorMessage errorMessage = new ErrorMessage(message); 
        return new ResponseEntity<>(errorMessage), HttpStatus.CONFLICT);
    }
}
@ControllerAdvice
公共类异常处理程序{
@ExceptionHandler(DataIntegrityViolationException.class)
公共响应性冲突(DataIntegrityViolationException e){
String message=NestedExceptionUtils.getmostSpecificCase(e.getMessage();
ErrorMessage ErrorMessage=新的ErrorMessage(消息);
返回新的ResponseEntity(errorMessage),HttpStatus.CONFLICT);
}
}
其中
ErrorMessage
是您的自定义错误消息类


其他信息:

能否提供引发异常的代码以及在何处捕获异常?当我编写repository.save()时,它违反了db约束并引发异常。感谢您的回答。但我不明白该把代码放在哪里?在你的项目中它是独立类。在我提供的链接中有更多的例子…当然是在我的项目中,但在哪一层?web层中的服务控制器(请参阅)。