spring - 如何从 SqlExceptionHelper 获取有关 REST spring 应用程序的错误信息
问题描述
我的数据库中有一些表,我想获取有关对数据库的错误请求的信息。如果我试图用错误的外键保存实体,我想获取有关这些键的详细信息。
例如:
2020-03-25 18:37:37.595 ERROR 9788 --- [nio-8090-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: insert or update on table "student" violates foreign key constraint "student_fkey_to_specialty"
Detail: Key (specialtykey)=(2) is not present in table "specialty".
我试图用这段代码解决,但我得到了其他信息。
could not execute statement; SQL [n/a]; constraint [student_fkey_to_specialty]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
我的代码:
@PostMapping
public void saveStudent(@RequestBody StudentDTO studentDTO) {
if(studentDTO!=null){
try {
studentService.save(studentDTO);
}catch (Exception|Error e){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getLocalizedMessage(),e );
}
}
}
解决方案
学生表的“student_fkey_to_specialty”被违反。检查此约束是哪个字段,并为该字段提供正确的值
推荐阅读
- http - Cannot make a successful curl put call in PHP
- java - JShell 的限制是什么?
- ssl - AWS 上的 Kubernetes:使用 nginx-ingress + cert-manager 保留客户端 IP
- oracle - Install Oracle Apex in Microsoft server 2016
- python - 从函数返回列表
- abap - 将 se16n 限制为某些表行
- node.js - How to check if field exists for a specified id in MongoDb
- node.js - Auto-increment based on two columns in Sequelize for postgres
- javassist - 使用 Javassist 检索私有方法信息
- j - 为什么右动词 ] 返回值而不是左动词 [?