Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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启动-错误处理_Spring_Spring Mvc_Error Handling_Spring Boot - Fatal编程技术网

Spring启动-错误处理

Spring启动-错误处理,spring,spring-mvc,error-handling,spring-boot,Spring,Spring Mvc,Error Handling,Spring Boot,我正试图在Spring引导中为我的控制器编写错误处理程序,以捕获大多数可能的错误(Spring、sql等)。到目前为止,我能够得到带有null的JSON响应,但是我无法将任何数据放入其中。当我试图在中获取错误消息时,我只收到一个空白页 import java.io.IOException; 导入java.sql.SQLException; 导入javax.servlet.http.HttpServletRequest; 导入org.springframework.boot.autoconfigu

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

import java.io.IOException;
导入java.sql.SQLException;
导入javax.servlet.http.HttpServletRequest;
导入org.springframework.boot.autoconfigure.web.ErrorController;
导入org.springframework.web.bind.annotation.ExceptionHandler;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RestController;
导入org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
@RestController
公共类BasicErrorController实现ErrorController{
私有静态最终字符串错误_PATH=“/ERROR”;
@请求映射(值=错误路径)
@ExceptionHandler(值={NoSuchRequestHandlingMethodException.class、SQLException.class、IOException.class、RuntimeException.class、Exception.class})
公共ErrorBody defaultErrorHandler(HttpServletRequest请求,异常e){
ErrorBody eBody=新的ErrorBody();
setMessage(例如getCause().getMessage());
返回乌木;
}
}
导入lombok.Getter;
进口龙目织机;
@吸气剂
@塞特
公营机构{
私有字符串日期时间;
私有字符串异常;
私有字符串url;
私有字符串消息;
}

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

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

这是@ExceptionHandler(Exception.class)的一个示例

您可以使用@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");
}
}

你可以这样做:

 @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"," ");
    }


}

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