Spring MVC @ExceptionHandler方法在控制器调用成功的情况下

时间:2016-02-05 11:21:26

标签: java spring spring-mvc

我试图捕捉错误情况,但我在所有情况下都会遇到异常。

以下是我的代码段:

@RequestMapping(value = "/config/file/data", method = RequestMethod.GET)
@ResponseBody
@ExceptionHandler(RestClientException.class)
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Not Found")
public void restClientException()
{
  //do nothing
}

public List<myfile> getAllmyfiles() throws RestClientException
{
    return myfileService.getAllmyfiles();
}

1 个答案:

答案 0 :(得分:0)

试试这个:

@ExceptionHandler(RestClientException.class)
@ResponseStatus(value = HttpStatus.NOT_FOUND, reason = "Not Found")
public void restClientException()
{
  //do nothing
}

@RequestMapping(value = "/config/file/data", method = RequestMethod.GET)
@ResponseBody   
public List<myfile> getAllmyfiles() throws RestClientException
{
    return myfileService.getAllmyfiles();
}

您不应将@RequestMapping@ExceptionHandler一起使用:当引发指定的异常时,会自动调用使用@ExceptionHandler注释的方法