ResponseEntityExceptionHandler在新的Spring Boot版本中出现错误。

huangapple go评论74阅读模式
英文:

ResponseEntityExceptionHandler error with new Spring Boot version

问题

虽然我之前在较早版本的Spring Boot中可以使用ResponseEntityExceptionHandler,就像在这个链接中展示的那样,但是当我在我的新应用程序中实现相同的异常处理程序(Spring Boot版本3.0.2)时,对于handleExceptionInternal方法,我会得到以下错误:

> 方法未覆盖其超类中的方法

所以,我可以通过不扩展ResponseEntityExceptionHandler来解决这个问题,但我不确定是否有更好的方法来解决这个问题。

对于这个问题有任何想法吗?

英文:

Although I could use ResponseEntityExceptionHandler with the previous Spring Boot versions as shown on this example, I get the following error for the handleExceptionInternal method when I implement the same exception handler in my new app (Spring Boot version 3.0.2):

> Method does not override method from its superclass

So, I can fix the problem by not extending ResponseEntityExceptionHandler, but I am not sure if there is a better way to fix this problem.

Any idea for this problem?

答案1

得分: 1

方法的签名不同,所以现在你得到了这个错误。检查签名这里

基本上你需要改变的是:HttpStatus --> HttpStatusCode

以下是覆盖该方法的示例:

 @Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex,
    Object body,
    HttpHeaders headers,
    HttpStatusCode statusCode,
    WebRequest request){
    
    // 在这里添加你的代码....

}
英文:

The signature of the method is different so now you get that error. Check signature here.

Basically what you need to change is: HttpStatus --> HttpStatusCode

Here is an example of overriding this method:

 @Override
protected ResponseEntity<Object> handleExceptionInternal(Exception ex,
    Object body,
    HttpHeaders headers,
    HttpStatusCode statusCode,
    WebRequest request){
    
    //your code here....

}

huangapple
  • 本文由 发表于 2023年2月18日 20:04:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定