首页>代码>SpringBoot上传excel文件解析并打印Excel数据>/form/src/main/java/vip/waitfor/form/controller/BaseController.java
package vip.waitfor.form.controller;


import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import vip.waitfor.form.controller.exception.RequestException;
import vip.waitfor.form.service.exception.*;
import vip.waitfor.form.util.ResponseResult;


import javax.servlet.http.HttpSession;

/**
 * 当前项目中所有控制器的基类
 */
public abstract class BaseController {

    /*
    正确响应时的代号
     */
    public static final Integer SUCCESS = 200;

    @ExceptionHandler({ServiceException.class, RequestException.class})
    @ResponseBody
    public ResponseResult<Void> handleException(Exception e) {
        Integer state = null;
        if (e instanceof DuplicatekeyException) {
            state = 400;//400-违反了Unique约束的异常
        } else if (e instanceof UserNotFoundException) {
            state = 401;//401-用户数据不存在
        } else if (e instanceof PasswordNotMatchException) {
            state = 402;//402-用户密码不正确
        } else if (e instanceof InsertException) {
            state = 500;//500-插入用户数据异常
        } else if (e instanceof UpdateException) {
            state = 501;//501-修改数据异常
        } else if (e instanceof DeleterException) {
            state = 502;//502-删除数据异常
        }
        return new ResponseResult<Void>(state, e);
    }

    /**
     * 从Session中获取uid
     *
     * @param session HttpSession对象
     * @return 当前登录的用户的id
     */
    protected Integer getUidFromSession(HttpSession session) {
        return Integer.valueOf(session.getAttribute("uid").toString());
    }

    /**
     * 从Session中获取uid
     *
     * @param session httpSession对象
     * @return 当前登录的用户的用户名
     */
    protected String getUsernameFromSession(HttpSession session) {
        return String.valueOf(session.getAttribute("username").toString());
    }
}
最近下载更多
wuyu8995861  LV7 2022年7月26日
shaoxf_nihao  LV4 2022年6月8日
Patrick1988  LV4 2022年1月14日
newscc  LV8 2021年10月18日
阿远远  LV8 2021年10月11日
zoujialuo  LV9 2021年10月8日
cp19791101  LV12 2021年9月4日
zhoayabin  LV1 2021年8月31日
最代码官方  LV168 2021年8月21日
最近浏览更多
暂无贡献等级
charleswang  LV7 2024年10月21日
微信网友_7134912998903808  LV15 2024年9月2日
HANCW  LV9 2024年8月8日
17669706126 2024年6月3日
暂无贡献等级
f22m1a2b2  LV17 2024年5月31日
WBelong  LV8 2024年3月29日
微信网友_6411774242443264 2023年11月29日
暂无贡献等级
lcqlcl  LV11 2023年10月31日
lyh1989  LV34 2023年10月24日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友