久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

@ControllerAdvice注解的使用,、全局異常捕獲,、自定義異常捕獲

 dabinglibrary 2019-08-20
@ControllerAdvice
public class DefaultExceptionHandler {

@ExceptionHandler({UnauthorizedException.class})
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public ModelAndView processUnauthenticatedException(NativeWebRequest request, UnauthorizedException e) {
ModelAndView mv = new ModelAndView();
mv.addObject("exception", e);
mv.setViewName("/Error/Unauthorized");
return mv;
}


@ExceptionHandler({RuntimeException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ModelAndView processRuntimeException(NativeWebRequest request, RuntimeException e) {
ModelAndView mv = new ModelAndView();
mv.addObject("exception", e);
mv.setViewName("/Error/Error");
return mv;
}

@ExceptionHandler({MessageException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ModelAndView processMessageException(NativeWebRequest request, MessageException e) {
ModelAndView mv = new ModelAndView();
mv.addObject("exception", e);
mv.setViewName("/Error/NotFound");
return mv;
}

@ExceptionHandler({DuplicateKeyException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ModelAndView processDuplicateKeyException(NativeWebRequest request, DuplicateKeyException e) {
ModelAndView mv = new ModelAndView();
mv.addObject("exception", e);
mv.setViewName("/Error/500");
return mv;
}

@ExceptionHandler({Exception.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ModelAndView processException(NativeWebRequest request, Exception e) {
ModelAndView mv = new ModelAndView();
mv.addObject("exception", e);
mv.setViewName("/Error/404");
return mv;
}

}

捕獲異常也可以返回json:

@ExceptionHandler
@ResponseBody
public APIManageBaseResp exceptionHandler(Exception e) {
logger.error("調(diào)用接口出現(xiàn)異常:{}",e);
APIManageBaseResp<String> resp = new APIManageBaseResp();
resp.setResultCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
if(e instanceof MessageException){
resp.setErrorMsg(e.getMessage());
}else{
resp.setErrorMsg("調(diào)用接口出現(xiàn)異常:"+e.getMessage());
}
resp.setDataFiled("");
return resp;
}


@RequestMapping(value = "/testFacade")
@ResponseBody
public String testFacade() {
int a = 1/0;

return "ok";
}


@RequestMapping(value = "/testFacade1")
@ResponseBody
public String testFacade1() throws MessageException {
throw new MessageException("錯了en ");

}


@RequestMapping(value = "/testFacade2")
@ResponseBody
public String testFacade2() {
throw new UnauthorizedException("錯了b ");

}


@RequestMapping(value = "/testFacade3")
@ResponseBody
public String testFacade3() {
throw new RuntimeException("錯了a ");

}

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式,、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,,請點擊一鍵舉報,。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多