Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 如何在Spring Boot上替换ErrorController不推荐的功能?_Spring Boot_Deprecated - Fatal编程技术网

Spring boot 如何在Spring Boot上替换ErrorController不推荐的功能?

Spring boot 如何在Spring Boot上替换ErrorController不推荐的功能?,spring-boot,deprecated,Spring Boot,Deprecated,在Spring boot上具有自定义错误控制器: package com.example.controllers; 导入org.springframework.stereotype.Controller; 导入org.springframework.web.bind.annotation.RequestMapping; 导入org.springframework.boot.web.servlet.error.ErrorController; 导入javax.servlet.http.HttpSe

在Spring boot上具有自定义错误控制器:

package com.example.controllers;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.boot.web.servlet.error.ErrorController;
导入javax.servlet.http.HttpServletRequest;
@控制器
公共类CustomErrorController实现ErrorController
{
@请求映射(“/error”)
公共字符串句柄错误(HttpServletRequest)
{
...
}
@凌驾
公共字符串getErrorPath()
{
返回“/错误”;
}
}
但是,当compile说:
ErrorController中的getErrorPath()已经被弃用了
。好的,我找到了信息:使用
server.error.path
property。好的,在
application.properties
中添加它并删除该函数,但是现在说:
CustomErrorController不是抽象的,并且不重写ErrorController中的抽象方法getErrorPath(),需要一个不推荐使用的函数吗


如何制作自定义错误控制器?
ErrorController
需要
getErrorPath
但它已被弃用,正确的替代方法是什么?

有一个
@ControllerAdvice
注释

@ControllerAdvice
public class MyErrorController {


   @ExceptionHandler(RuntimeException.class)
   public String|ResponseEntity|AnyOtherType handler(final RuntimeException e) {
     .. do handle ..
   }

   @ExceptionHandler({ Exception1.class, Exception2.class })
   public String multipleHandler(final Exception e) {

   }

}
  • 要处理错误,无需定义控制器类 实现一个错误控制器
  • 处理整个应用程序中的错误,而不是编写

    @Controller
     public class CustomErrorController implements ErrorController{
      @RequestMapping("/error")
      public String handleError(HttpServletRequest request)
        {
        ...
        }
      }
    
  • 使用下面的类

    @ControllerAdvice
     public class myExceptionHandler extends ResponseEntityExceptionHandler {
    
      @ExceptionHandler(Exception.class)
      public final ResponseEntity<YourResponseClass> handleAllExceptions(Exception ex, WebRequest request) {
        YourResponseClassexceptionResponse = new YourResponseClass(new Date(), ex.getMessage());// Its an example you can define a class with your own structure
        return new ResponseEntity<>(exceptionResponse, HttpStatus.INTERNAL_SERVER_ERROR);
      }
    
      @ExceptionHandler(CustomException.class)
      public final ResponseEntity<YourResponseClass> handleAllExceptions(Exception ex, WebRequest request) {
        YourResponseClass exceptionResponse = new YourResponseClass(new Date(), ex.getMessage()); // For reference 
        return new ResponseEntity<YourResponseClass>(exceptionResponse, HttpStatus.INTERNAL_SERVER_ERROR);
      }
    
       @ExceptionHandler(BadCredentialsException.class)
      public final ResponseEntity<YourResponseClass> handleBadCredentialsException(BadCredentialsException ex, WebRequest request){
            YourResponseClass exceptionResponse = new YourResponseClass(new Date(), ex.getMessage());// For refernece 
                return new ResponseEntity<>(exceptionResponse, HttpStatus.UNAUTHORIZED);          
      }  
    
    @ControllerAdvice
    公共类myExceptionHandler扩展了ResponseEntityExceptionHandler{
    @ExceptionHandler(Exception.class)
    公共最终响应处理异常(异常,例如WebRequest请求){
    YourResponseClassexceptionResponse=new YourResponseClass(new Date(),例如getMessage());//这是一个可以用自己的结构定义类的示例
    返回新的ResponseEntity(exceptionResponse,HttpStatus.INTERNAL_服务器_错误);
    }
    @ExceptionHandler(CustomException.class)
    公共最终响应处理异常(异常,例如WebRequest请求){
    YourResponseClass exceptionResponse=new YourResponseClass(new Date(),例如getMessage());//供参考
    返回新的ResponseEntity(exceptionResponse,HttpStatus.INTERNAL_服务器_错误);
    }
    @ExceptionHandler(BadCredentialsException.class)
    公共最终响应标题handleBadCredentialsException(BadCredentialsException ex,WebRequest请求){
    YourResponseClass exceptionResponse=新建YourResponseClass(新日期(),例如getMessage());//用于引用
    返回新的ResponseEntity(exceptionResponse,HttpStatus.UNAUTHORIZED);
    }  
    
    }

  • 上面用@ControllerAdvice注释的类充当自定义异常处理程序,它处理应用程序引发的所有Expection。在上面的代码示例中,为了便于理解,只显示了三个异常。它可以处理许多异常

  • 在您的应用程序中,如果抛出任何异常,它将返回此类并发送响应。您可以根据您的需要定制消息和结构


  • 从2.3.x版开始,Spring boot已弃用此方法。只需返回null,因为它无论如何都会被忽略。如果希望在完全删除方法时防止将来的编译错误,请不要使用@Override注释。如果需要,也可以抑制不推荐警告,但是,该警告(也是@Override注释)有助于提醒您在删除该方法时清理/修复代码

    @Controller
    @RequestMapping("/error")
    @SuppressWarnings("deprecation")
    public class CustomErrorController implements ErrorController {
    
         public String error() {
            // handle error
            // ..
         }
    
         public String getErrorPath() {
             return null;
         }
    }
    

    也许你应该看看
    BasicErrorController
    谢谢。有关于它的使用的文件吗?我已经在谷歌和论坛上搜索了使用示例,但我没有发现任何实用的东西,只有含糊不清的地方。谢谢,但不起作用<代码>错误解析模板[Error],模板可能不存在,或者任何已配置的模板解析程序都可能无法访问该模板
    ,但需要返回每个错误类型的自定义视图,例如
    片段/errors/404
    。我已将我的标记为@Override和@Deprecated,以充分利用这两个世界,现在修复编译器错误,稍后编译器错误将提醒我在可能时删除它。