【问题标题】:Spring-Boot - Error HandlingSpring-Boot - 错误处理
【发布时间】:2015-02-13 13:51:18
【问题描述】:

我正在尝试在 Spring-Boot 中为我的控制器编写错误处理程序,以捕获大多数可能的错误(Spring、sql 等)。到目前为止,我能够使用 Nulls 获得 JSON 响应,但是我无法将任何数据放入其中。当我尝试在其中收到错误消息时,我只收到一个空白页。

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;

@RestController
public class BasicErrorController implements ErrorController {
    private static final String ERROR_PATH = "/error";

    @RequestMapping(value=ERROR_PATH)
    @ExceptionHandler(value = {NoSuchRequestHandlingMethodException.class, SQLException.class, IOException.class, RuntimeException.class, Exception.class})
    public ErrorBody defaultErrorHandler(HttpServletRequest request, Exception e) {     
        ErrorBody eBody = new ErrorBody();         
        eBody.setMessage(e.getCause().getMessage());
        return eBody;
    }
}
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ErrorBody {
    private String dateTime;
    private String exception;
    private String url;
    private String message;
}

【问题讨论】:

  • 不确定您所说的“无法将任何数据放入其中”是什么意思 - 您做了什么,结果是什么?您不需要 ErrorController 中的 @ExceptionHandler 注释(但您确实需要实现接口,而您的似乎不需要)。看看BasicErrorController在Spring Boot中是如何实现的,寻找线索。
  • 实现ErrorAttributes(不是ErrorController)可能会更好,但如果您想要完全控制,这是您的选择。
  • 感谢您的回答。 “无法将任何数据放入其中”是指每当我尝试从错误 JSON 中获取任何数据时都不会显示。我今天解决了它,我能够通过使用“HttpServletRequest 请求”并从请求中读取信息来获取有关错误的数据并将它们正确地以 json 格式发送。

标签: spring spring-mvc error-handling spring-boot


【解决方案1】:

你可以这样做:

 @ControllerAdvice
   public class ControllerExceptionTranslator {


    @ExceptionHandler(EntityNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ResponseBody
    SimpleErrorMessage handleException(EntityNotFoundException exception){
        log.debug("Entity Not Found Exception {}",exception.getMessage());
        log.trace(exception.getMessage(),exception);
        return new SimpleErrorMessage("Entity not found","This resource was not found");
    }


    @ExceptionHandler({UsernameNotFoundException.class})
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    SimpleErrorMessage handleException(UsernameNotFoundException exception){
        log.debug("Username not found {}",exception.getLocalizedMessage());
        log.trace(exception.getMessage(),exception);
        return new SimpleErrorMessage("Unaouthorized"," ");
    }


}

【讨论】:

    【解决方案2】:

    通过使用“HttpServletRequest 请求”并从请求中读取信息,我能够获取有关错误的数据并将它们正确地以 json 格式发送。

    @RequestMapping(value = ERROR_PATH)
        public ErrorBody defaultErrorHandler(HttpServletRequest request) {....}
    

    【讨论】:

      【解决方案3】:

      这里是@ExceptionHandler(Exception.class)的例子

      https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc

      你可以使用@ControllerAdvice

      package demo.controller;
      
      import org.springframework.web.bind.WebDataBinder;
      import org.springframework.web.bind.annotation.ControllerAdvice;
      import org.springframework.web.bind.annotation.ExceptionHandler;
      import org.springframework.web.bind.annotation.InitBinder;
      import org.springframework.web.bind.annotation.ModelAttribute;
      
      @ControllerAdvice
      public class ExceptionControllerAdvice {
      
       @InitBinder
       public void initBinder(WebDataBinder binder) {
          System.out.println("controller advice: init binder");
      }
      
      
      @ExceptionHandler(Exception.class)
      public String exception(Exception e) {
          System.out.println("controller advice: exception Handler");
          System.out.println(e.getMessage());
          return "error";
      }
      
      @ModelAttribute
      public void modelAttribute(){
          System.out.println("controller advice:model Attribute");
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-06-13
        • 1970-01-01
        • 2022-06-17
        • 2020-10-08
        • 1970-01-01
        • 2018-12-09
        • 2017-07-02
        • 2023-03-26
        • 1970-01-01
        相关资源
        最近更新 更多