2016-02-05 30 views
0

我試圖捕獲一個錯誤場景,但在所有情況下我都得到一個異常。在成功的情況下調用控制器的Spring MVC @ExceptionHandler方法

下面是我的代碼片段:

@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(); 
} 
+0

發佈一些測試或例外。 getAllmyfiles()在任何地方都被調用,並且沒有用@RequestMapping註釋,如果你想要一個答案,請添加更多的上下文 –

+0

更新我的代碼..在成功的情況下獲取數組列表返回Arrays.asList(myfile);但在控制器不知道如何exceptionhandler調用 – JOGO

回答

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註釋的方法自動調用時指定的異常拋出

+0

aaha ..非常感謝.. .. :) – JOGO

相關問題