Sqlexception永远不会仅仅投掷全球例外
我正试图在MySQL中引起错误,以在春季获得Sqlexception,但没有成功。 Sqlexception永远不会仅引发全球例外。 我正在尝试删除键入不允许删除的名称的记录,在mysql中,这是一个错误:“错误代码:1451。无法删除或更新父行。” 这在mysql中应该这样。当我尝试True Controller 时,我得到了500个错误响应,只是异常,应该是SQLEXCEPTION。
我正在删除方法中的代码:
@Override
public void deleteCity(String name)throws SQLException {
if(cityRepo.findByName(name)==null)throw new NullPointerException("No city with entered name");
City city = cityRepo.findByName(name);
cityRepo.delete(city);
}
这是控制器:
@DeleteMapping(path = "/city/delete")
public void deleteCity(@Valid @NotEmpty(message = "Name must not be empty")
@RequestParam(value = "name", required = true) String name) throws SQLException {
cityService.deleteCity(name);
}
Global Handler:Global Handler:
@ExceptionHandler(SQLException.class)
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
public ResponseEntity<Object> handleArgumentException(SQLException sqlExc, WebRequest request) {
log.error("SQL database exception occurred - check database", sqlExc);
return buildErrorResponse(sqlExc, HttpStatus.NOT_ACCEPTABLE, request);
}
我尝试了我的课程, Sqlexception,但这是相同的行为。有人知道这个问题吗?
I am trying to provoke error in MySQL to get SQLException in spring but without success. SQLException is never thrown only global Exception. I am trying to delete record typing a name which is not allowed to delete and in MySQL it is a error: "Error Code: 1451. Cannot delete or update a parent row.." This is in MySQL and it should be like this. When I try it true controller I got 500 error response, just Exception and it should be SQLException.
I am putting code from delete method:
@Override
public void deleteCity(String name)throws SQLException {
if(cityRepo.findByName(name)==null)throw new NullPointerException("No city with entered name");
City city = cityRepo.findByName(name);
cityRepo.delete(city);
}
Here is controller:
@DeleteMapping(path = "/city/delete")
public void deleteCity(@Valid @NotEmpty(message = "Name must not be empty")
@RequestParam(value = "name", required = true) String name) throws SQLException {
cityService.deleteCity(name);
}
Global handler:
@ExceptionHandler(SQLException.class)
@ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
public ResponseEntity<Object> handleArgumentException(SQLException sqlExc, WebRequest request) {
log.error("SQL database exception occurred - check database", sqlExc);
return buildErrorResponse(sqlExc, HttpStatus.NOT_ACCEPTABLE, request);
}
I tried with my class like to extend SQLException but it is same behavior. Anyone knows the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您尝试将其放在您的CityService
上
希望这项工作
Should you try to put this at your CityService
It's will tell Spring Boot how to handle SQLException
hope it's work