为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 435 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号