Sqlexception永远不会仅仅投掷全球例外

发布于 2025-02-10 03:51:41 字数 1265 浏览 0 评论 0原文

我正试图在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷清清 2025-02-17 03:51:41

如果您尝试将其放在您的CityService

@ExceptionHandler(SQLException.class)
public void handleSQLException() {}



希望这项工作

Should you try to put this at your CityService

@ExceptionHandler(SQLException.class)
public void handleSQLException() {}

It's will tell Spring Boot how to handle SQLException

hope it's work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文