Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.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/2/spring/13.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
Java 如何在Spring REST中捕获CannotGetJdbcConnectionException_Java_Spring_Rest_Spring Mvc - Fatal编程技术网

Java 如何在Spring REST中捕获CannotGetJdbcConnectionException

Java 如何在Spring REST中捕获CannotGetJdbcConnectionException,java,spring,rest,spring-mvc,Java,Spring,Rest,Spring Mvc,我正在使用Spring和OAuth创建一些restfulweb服务。当数据库关闭时,框架中的某个地方a无法获取jdbcconnectionexception——当我说框架中的某个地方时,我的意思是它是由Spring代码引起的,而不是由我编写的代码引起的。我得到的其他回应是: { "timestamp": 1467982842783 "status": 500 "error": "Internal Server Error" "exception": "org.springframework.jd

我正在使用
Spring
OAuth
创建一些restfulweb服务。当数据库关闭时,框架中的某个地方a
无法获取jdbcconnectionexception
——当我说框架中的某个地方时,我的意思是它是由Spring代码引起的,而不是由我编写的代码引起的。我得到的其他回应是:

{
"timestamp": 1467982842783
"status": 500
"error": "Internal Server Error"
"exception": "org.springframework.jdbc.CannotGetJdbcConnectionException"
"message": "Could not get JDBC Connection; nested exception is java.sql.SQLException: Network error IOException: Connection refused: connect"
"path": "/1/test"
}
我试过很多东西。例如,我尝试创建一个扩展
ResponseEntityExceptionHandler
的类,如下所示:

@ControllerAdvice
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {
    @ExceptionHandler(CannotGetJdbcConnectionException.class)
    public ResponseEntity<String> handleNoConnection() {
        return new ResponseEntity<String>("Unable to connect to the database. Is it running?", HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

我不知道还能做什么。正如我所说,我无法直接捕获异常,因为它发生在Spring代码本身中。是否有其他方法捕获此异常?

您的“状态”:500 accurse(由于内部服务器错误)并不明确表示没有连接。服务器可能会返回此错误,例如,当它找不到正确的匹配项以及要调用的确切方法时。检查您的拖地请求是否正确correct@v_haruty我的问题与500错误无关。我的问题是如何捕获在我的代码之外发生的异常——在Spring框架内部。在本例中,发生了CannotGetJdbcConnectionException异常,我希望捕获它。
@Controller
public class ErrorController extends BasicErrorController {

    public ErrorController() {
        super(new DefaultErrorAttributes());
    }   

    @Override public org.springframework.http.ResponseEntity<java.util.Map<String,Object>> error(HttpServletRequest request) {
        Map<String, Object> body = getErrorAttributes(request,
                isIncludeStackTrace(request, MediaType.ALL));
        HttpStatus status = getStatus(request);
        return new ResponseEntity<Map<String, Object>>(body, status);
    };

    @RequestMapping(
            value = {"${error.path:/error}"},
            produces = {"text/html"}
    )
    public ModelAndView errorHtml(HttpServletRequest request) {


        final HttpStatus status = getStatus(request);
        if (status == HttpStatus.UNAUTHORIZED) {
            return new ModelAndView("error401");
        } else if (status == HttpStatus.FORBIDDEN) {
            return new ModelAndView("error403");
        } else {
            return new ModelAndView("error500");
        }
    }

    public HttpStatus getStatus(HttpServletRequest request) {
        Integer statusCode = (Integer)request.getAttribute("javax.servlet.error.status_code");
        if(statusCode != null) {
            try {
                return HttpStatus.valueOf(statusCode.intValue());
            } catch (Exception e) {
            }
        }
        return HttpStatus.INTERNAL_SERVER_ERROR;
    }
}