亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

springboot中的錯誤處理

springboot中的錯誤處理

郎朗坤 2019-03-22 19:15:17
springboot默認會用whitelabel來顯示錯誤視圖,可以實現ErrorController來自定義.@Controllerpublic class ErrorHandleController implements ErrorController {    @Override    public String getErrorPath() {        return "error";    }    @RequestMapping    public String errorHandle() {        return getErrorPath();    }}自己定義了這么一個處理器,結果把css,js這里當錯誤給處理了,這么解決???
查看完整描述

1 回答

?
夢里花落0921

TA貢獻1772條經驗 獲得超6個贊

你可以直接定義一個錯誤處理器,在service層拋出相關的自定義異常,然后在PersonalExceptionHandler進行相關的處理。


 @ControllerAdvice

   public class PersonalExceptionHandler {



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

    }

}


查看完整回答
反對 回復 2019-04-16
  • 1 回答
  • 0 關注
  • 452 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號