SpringBoot统一异常处理研究

系统对异常的处理使用统一的异常处理流程:1、自定义异常类型。2、自定义错误代码及错误信息。3、对于可预知的异常由程序员在代码中主动抛出,由SpringMVC统一捕获。 可预知异常是程序员在代码中手动抛出本系统定义的特定异常类型,由于是程序员抛出的异常,通常异常信息比较齐全,程序员在抛出时会指定错误代码及错误信息,获取异常信息也比较方便。4、对于不可预知的异常(运行时异常)由SpringMVC统一捕获Exception类型的异常。 不可预知异常通常是由于系统出现bug、或一些不要抗拒的错误(比如网络中断、服务器宕机等),异常类型为RuntimeException类型(运行时异常)。5、可预知的异常及不可预知的运行时异常最终会采用统一的信息格式(错误代码+错误信息)来表示,最终也会随请求响应给客户端。1、在controller、service、dao中程序员抛出自定义异常;springMVC框架抛出框架异常类型2、统一由异常捕获类捕获异常,并进行处理3、捕获到自定义异常则直接取出错误代码及错误信息,响应给用户。4、捕获到非自定义异常类型首先从Map中找该异常类型是否对应具体的错误代码,如果有则取出错误代码和错误信息并响应给用户,如果从Map中找不到异常类型所对应的错误代码则统一为99999错误代码并响应给用户。5、将错误代码及错误信息以Json格式响应给用户。错误处理实战在共有项目中common中建exception包专门来存错误处理类.CustomExceptionpublic class CustomException extends RuntimeException {    private ResultCode resultCode;    public CustomException(ResultCode resultCode){        //异常信息 错误代码+异常信息        super("错误代码:"+resultCode.code()+"错误信息:"+resultCode.message());        this.resultCode=resultCode;    }    public ResultCode getResultCode(){        return this.resultCode;    }}其中作为响应的结果的ResultCode接口为 定义了基础信息:public interface ResultCode {    //操作是否成功,true为成功,false操作失败    boolean success();    //操作代码    int code();    //提示信息    String message();}ExceptionCastpublic class ExceptionCast {    //使用静态方法抛出自定义异常    public static void cast(ResultCode resultCode){        throw new CustomException(resultCode);    }}ExceptionCatch@ControllerAdvicepublic class ExceptionCatch {    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionCatch.class);    //使用EXCEPTIONS存放异常类型和错误代码的映射,ImmutableMap的特点的一旦创建不可改变,并且线程安全    private static ImmutableMap<Class<? extends Throwable>,ResultCode> EXCEPTIONS;    //使用builder来构建一个异常类型和错误代码的异常    protected static ImmutableMap.Builder<Class<? extends Throwable>,ResultCode> builder = ImmutableMap.builder();    @ExceptionHandler(Exception.class)    @ResponseBody    public ResponseResult exception(Exception e){        LOGGER.error("catch exception : {}\r\nexception: ",e.getMessage(), e);        if(EXCEPTIONS == null)            EXCEPTIONS = builder.build();        ResultCode resultCode = EXCEPTIONS.get(e.getClass());        ResponseResult responseResult;        if (resultCode != null) {            responseResult = new ResponseResult(resultCode);        } else {            responseResult = new ResponseResult(CommonCode.SERVER_ERROR);        }        return responseResult;    }    //捕获CustomEcception异常    @ExceptionHandler(CustomException.class)    @ResponseBody    public ResponseResult customException(CustomException e) {        LOGGER.error("catch exception : {}\\r\\nexception: ", e.getMessage(), e);        ResultCode resultCode = e.getResultCode();        ResponseResult responseResult = new ResponseResult(resultCode);        return responseResult;    }    static{        //在这里加入一些基础的异常类型判断        builder.put(HttpMessageNotReadableException.class,CommonCode.INVALID_PARAM);    }}CommonCodeimport lombok.ToString;@ToStringpublic enum CommonCode implements ResultCode{    INVALID_PARAM(false,10003,"非法参数!"),    SUCCESS(true,10000,"操作成功!"),    FAIL(false,11111,"操作失败!"),    UNAUTHENTICATED(false,10001,"此操作需要登陆系统!"),    UNAUTHORISE(false,10002,"权限不足,无权操作!"),    SERVER_ERROR(false,99999,"抱歉,系统繁忙,请稍后重试!");//    private static ImmutableMap<Integer, CommonCode> codes ;    //操作是否成功    boolean success;    //操作代码    int code;    //提示信息    String message;    private CommonCode(boolean success,int code, String message){        this.success = success;        this.code = code;        this.message = message;    }    @Override    public boolean success() {        return success;    }    @Override    public int code() {        return code;    }    @Override    public String message() {        return message;    }}CmsCodeimport lombok.ToString;@ToStringpublic enum CmsCode implements ResultCode {    CMS_ADDPAGE_EXISTSNAME(false,24001,"页面名称已存在!"),    CMS_GENERATEHTML_DATAURLISNULL(false,24002,"从页面信息中找不到获取数据的url!"),    CMS_GENERATEHTML_DATAISNULL(false,24003,"根据页面的数据url获取不到数据!"),    CMS_GENERATEHTML_TEMPLATEISNULL(false,24004,"页面模板为空!"),    CMS_GENERATEHTML_HTMLISNULL(false,24005,"生成的静态html为空!"),    CMS_GENERATEHTML_SAVEHTMLERROR(false,24006,"保存静态html出错!"),    CMS_COURSE_PERVIEWISNULL(false,24007,"预览页面为空!");    //操作代码    boolean success;    //操作代码    int code;    //提示信息    String message;    private CmsCode(boolean success, int code, String message){        this.success = success;        this.code = code;        this.message = message;    }    @Override    public boolean success() {        return success;    }    @Override    public int code() {        return code;    }    @Override    public String message() {        return message;    }}服务层add方法添加错误处理public CmsPageResult add(CmsPage cmsPage) {    if (cmsPage==null)        ExceptionCast.cast(CommonCode.INVALID_PARAM);//抛出通用错误类中的无效参数    //检验页面是否存在 根据页面名称 站点id 页面path查询    CmsPage cmsPage1 = cmsPageRepository.findByPageNameAndSiteIdAndPageWebPath(cmsPage.getPageName(), cmsPage.getSiteId(), cmsPage.getPageWebPath());    /*if (cmsPage1 == null) {        cmsPage.setPageId(null);//添加页面主键由spring data自动生成        cmsPageRepository.save(cmsPage);        //返回结果        CmsPageResult cmsPageResult = new CmsPageResult(CommonCode.SUCCESS, cmsPage);        return cmsPageResult;    }else { //如果不等于null就是有页面 添加失败抛出已存在页面异常        ExceptionCast.cast(CmsCode.CMS_ADDPAGE_EXISTSNAME);    }    return new CmsPageResult(CommonCode.FAIL, null);//如果存在则返回null 失败信息*/    //先列出所有错误再执行正确执行结果    if (cmsPage1!=null){        ExceptionCast.cast(CmsCode.CMS_ADDPAGE_EXISTSNAME);    }    cmsPage.setPageId(null);//添加页面主键由spring data自动生成    cmsPageRepository.save(cmsPage);    //返回结果    return new CmsPageResult(CommonCode.SUCCESS, cmsPage);}注意:启动类上要加上扫描异常处理类所在位置的包,例如:@ComponentScan(basePackages={"com.xuecheng.framework"})

SpringBoot统一异常处理研究

Natural-Imagine Dragons梦龙乐队

《Natural》是美国乐队组合Imagine Dragons演唱的一首歌曲,收录于Imagine Dragons2018年7月17日发行的专辑《Natural》中。歌词(来自百度百科) 歌曲播放在最下方:    Well, you hold the line  只有你还没有放弃  When every one of them is giving up or giving in, tell me  当其他所有人都停止了尝试 被挫折磨尽了希望  In this house of mine  我所在之处  Nothing ever comes without a consequence or cost, tell me  凡事皆有因果报应 没有什么得来轻而易举 所以告诉我  Will the stars align?  星星是否会排列成线  Will heaven step in? Will it save us from our sin? Will it?  上帝之手是否会介入 更改冥冥中的定数 将我们从犯下的罪恶中解救'Cause this house of mine stands strong  而我无所顾忌 因为我建立的国度会永恒伫立  That's the price you pay  这就是你要付出的代价  Leave behind your heartache, cast away  不要沉溺于眼下的心碎 抛之脑后告诉自己  Just another product of today  这不过是今天遇到的又一小事而已  Rather be the hunter than the prey  别再学猎物般逃窜 要像猎人一样主动出击  And you're standing on the edge,  现在你站在悬崖的边缘  Face up 'cause you're a  抬起头面对 因为你  Natural  生来如此  A beating heart of stone  就像坚石般强有力的心跳  You gotta be so cold  你要学着变得冷酷坚硬  To make it in this world  在这个世界寻到自己的立足之地  Yeah, you're a natural  这就是你的天性使然  Living your life cutthroat  在残酷的竞争中生存  You gotta be so cold  你要学着变得冷酷坚硬  Yeah, you're a natural  是的 这就是你的天性  Will somebody  是否有人能够  Let me see the light within the dark trees' shadows and  让我看见穿过窸窣树影透下的斑驳的光  What's happenin'?  发生了什么  Lookin' through the glass find the wrong  透过放大镜的镜片仔细找寻着  Within the past knowin'  过去的错误的足迹  Oh, we are the youth  我们年轻气盛 意气风发  Call out to the beast,  肆意释放内心隐藏的兽性  Not a word without the peace, facing  而嘴里的话语却不忘和平  A bit of the truth, the truth  而这只是你尚需了解的冰山一角  That's the price you pay  这就是你要付出的代价  Leave behind your heartache, cast away  不要沉溺于眼下的心碎 抛之脑后告诉自己  Just another product of today  这不过是今天遇到的又一小事而已  Rather be the hunter than the prey  别再学猎物般逃窜 要像猎人一样主动出击  And you're standing on the edge,  现在你站在悬崖的边缘  Face up 'cause you're a  抬起头面对 因为你  Natural  生来如此  A beating heart of stone  就像坚石般强有力的心跳  You gotta be so cold  你要学着变得冷酷坚硬  To make it in this world  在这个世界寻到自己的立足之地  Yeah, you're a natural  这就是你的天性使然  Living your life cutthroat  在残酷的竞争中生存  You gotta be so cold  你要学着变得冷酷坚硬  Yeah, you're a natural  是的 这就是你的天性  Deep inside me, I'm fading to black, I'm fading  我的内心深处 正在一点一点被黑色侵蚀吞没  Took an oath by the blood of my hand, won't break it  即使用我手上的血所立的誓约 也无法改变这一切  I can taste it, the end is upon us, I swear  我能够感觉到 结局的走向由我们掌舵 我发誓我一定会做到  Gonna make it  不顾一切地成功  I'm gonna make it  我会不顾一切地成功  Natural  生来如此  A beating heart of stone  就像坚石般强有力的心跳  You gotta be so cold  你要学着变得冷酷坚硬  To make it in this world  在这个世界寻到自己的立足之地  Yeah, you're a natural  这就是你的天性使然  Living your life cutthroat  在残酷地竞争中生存  You gotta be so cold  你要学着变得冷酷坚硬  Yeah, you're a natural  是的 这就是你的天性  Natural  生来如此  Yeah, you're a natural  是的 这就是你的天性

Natural-Imagine Dragons梦龙乐队

Java写Windows电脑开机自动访问上网验证页面

只针对于一些需要在登录页面输入账户名密码才能上网的情形。Java写的,打包成jar用bat运行即可,然后在Windows计划任务中将bat加为开机或登录启动即可。public class Main {    //连接    public static boolean isReachable(String remoteInetAddr) {        boolean reachable = false;        InetAddress address = null;        try {            address = InetAddress.getByName(remoteInetAddr);            reachable = address.isReachable(5000);        } catch (UnknownHostException e) {        } catch (IOException e) {        }        return reachable;    }    public static void main(String[] args) {        while (true) {            Boolean bon = false;            bon = isReachable("baidu.com");            if (bon)                return;            conn(bon);        }    }    //打开URL    public static void conn(boolean bon) {        URL url = null;        if (!bon) {            try {                url = new URL("这里填可以一键验证的网址 参数等可按F12获取");                InputStream in = url.openStream();//打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream                System.out.println("重连成功");                in.close();//关闭此输入流并释放与该流关联的所有系统资源。            } catch (IOException e) {                System.out.println("无法连接到:" + url.toString());            }        }    }}