2017-08-17 70 views
-1

我是一個初學者的啓動。代碼如下春季啓動一些路徑變量丟失,但返回404

@RestController 
@RequestMapping("/channel") 

public class ChannelController 
{ 
    @GetMapping("/getId/{participantId}/{walletType}") 
    Map<String,Object> getChannelId(@PathVariable Integer participantId , @PathVariable Integer walletType){ 
     return demoDAOImpl.getChannel(participantId, walletType); 
    } 
} 

當我打http://localhost:8080/channel/getId///,它返回默認白色標籤錯誤頁面,其中指出

有意外的錯誤(類型=未找到,狀態= 404)。

這裏我很困惑。我認爲Spring MVC應該拋出一個MissingPathVariableException。爲什麼它響應404響應,找不到資源?

+0

我使用的引導1.5.6,THX – aeromic

+0

這是什麼'PathVariableMissingException'你說的?爲什麼要拋出? –

+0

對於輸入錯誤,MissingPathVariableException是一個Spring內置異常,「通常這意味着URI模板與方法參數中聲明的路徑變量名稱不匹配。」(來自Spring原始註釋)。 – aeromic

回答

-1

基礎上,你的URL映射:你不能留空白,如 「///」 它必須是 「/數字/數字/

http://localhost:8080/channel/getId/0/0/

+0

這沒問題,問題是如何捕獲異常路徑變量缺少返回友好的消息給客戶端? – aeromic

+0

所以你應該搜索一下如何自定義spring-boot 404頁面 –

+0

不行,希望趕上路徑變量丟失異常,然後返回一些消息給客戶端,thx – aeromic

-1

我猜你在問什麼是定製異常。那麼你需要的是提供一個處理程序,你可以簡單地擴展抽象類ResponseEntityExceptionHandler並覆蓋handleMissingPathVariable方法。

一個例子:

@Override 
protected ResponseEntity<Object> handleMissingPathVariable(MissingPathVariableException ex, HttpHeaders headers, 
     HttpStatus status, WebRequest request) { 

    String error = ex.getParameter() + " parameter is missing"; 
    ErrorMessageHandler error =message(HttpStatus.BAD_REQUEST, ex.toString(), error) ; 
    return new ResponseEntity<Object>(error, new HttpHeaders(), HttpStatus.BAD_REQUEST); 

}