码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot项目开发(三)——统一响应对象、统一异常处理

时间:2020-12-09 12:29:35      阅读:9      评论:0      收藏:0      [点我收藏+]

标签:catch   autowired   try   nbsp   get   信息   OLE   user   move   

一、统一响应对象

项目开发中返回统一的数据格式有利于统一前后台开发规范

1、编写统一响应对象

package com.learn.mall.common;

import com.learn.mall.exception.LearnMallExceptionEnum;

/**
 * 通用返回对象
 *
 * @param <T>
 */
public class ApiRestResponse<T> {
    //状态码
    private Integer status;
    //信息
    private String msg;
    //返回数据
    private T data;
    private static final int OK_CODE = 10000;
    private static final String OK_MSG = "SUCCESS";

    public ApiRestResponse(Integer status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public ApiRestResponse(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public ApiRestResponse() {
        this(OK_CODE, OK_MSG);
    }

    public static <T> ApiRestResponse<T> success() {
        return new ApiRestResponse<>();
    }

    public static <T> ApiRestResponse<T> success(T result) {
        ApiRestResponse<T> response = new ApiRestResponse<>();
        response.setData(result);
        return response;
    }

    public static <T> ApiRestResponse<T> error(LearnMallExceptionEnum em) {
        return new ApiRestResponse<>(em.getCode(), em.getMsg());
    }

    public static <T> ApiRestResponse<T> error(Integer code, String msg) {
        return new ApiRestResponse<>(code, msg);

    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static int getOkCode() {
        return OK_CODE;
    }

    public static String getOkMsg() {
        return OK_MSG;
    }

    @Override
    public String toString() {
        return "ApiRestResponse{" +
                "status=" + status +
                ", msg=‘" + msg + ‘\‘‘ +
                ", data=" + data +
                ‘}‘;
    }
}

2、使用方式

 在controller层中使用统一的响应对象

package com.learn.mall.controller;

import com.learn.mall.common.ApiRestResponse;
import com.learn.mall.common.Constant;
import com.learn.mall.exception.LearnMallException;
import com.learn.mall.exception.LearnMallExceptionEnum;
import com.learn.mall.model.pojo.User;
import com.learn.mall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Autowired
    UserService userService;

     * 用户注册
     *
     * @param userName
     * @param password
     * @return
     * @throws LearnMallException
     */
    @PostMapping("/register")
    @ResponseBody
    public ApiRestResponse register(@RequestParam("userName") String userName,
                                    @RequestParam("password") String password) throws LearnMallException {
        //用户名不能为空
        if (StringUtils.isEmpty(userName)) {
            return ApiRestResponse.error(LearnMallExceptionEnum.NEED_USER_NAME);
        }
        //密码不能为空
        if (StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(LearnMallExceptionEnum.NEED_PASSWORD);
        }
        //密码长度不能小于8
        if (password.length() < 8) {
            return ApiRestResponse.error(LearnMallExceptionEnum.PASSWORD_TOO_SHORT);
        }
        //开始注册
        userService.register(userName, password);
        return ApiRestResponse.success();
    }

    /**
     * 用户登录
     * @param userName
     * @param password
     * @param session
     * @return
     * @throws LearnMallException
     */
    @PostMapping("/login")
    @ResponseBody
    public ApiRestResponse login(@RequestParam("userName") String userName,
                                 @RequestParam("password") String password, HttpSession session) throws LearnMallException {
        //用户名不能为空
        if (StringUtils.isEmpty(userName)) {
            return ApiRestResponse.error(LearnMallExceptionEnum.NEED_USER_NAME);
        }
        //密码不能为空
        if (StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(LearnMallExceptionEnum.NEED_PASSWORD);
        }
        //进行登录逻辑
        User user = userService.login(userName,password);
        //返回用户信息时不展示密码信息
        user.setPassword(null);
        //将用户信息存入session中
        session.setAttribute(Constant.USER,user);
        return ApiRestResponse.success(user);
    }

    /**
     * 更新用户签名
     * @param session
     * @param signature
     * @return
     * @throws LearnMallException
     */
    @PostMapping("/user/update")
    @ResponseBody
    public ApiRestResponse updateUserInfo(HttpSession session,@RequestParam String signature) throws LearnMallException {
        User currentUser = (User) session.getAttribute(Constant.USER);
        if(currentUser == null){
            return ApiRestResponse.error(LearnMallExceptionEnum.NEED_LOGIN);
        }
        User user = new User();
        user.setId(currentUser.getId());
        user.setPersonalizedSignature(signature);
        userService.updateUserInfo(user);
        return ApiRestResponse.success();
    }

    /**
     * 用户登出
     * @param session
     * @return
     */
    @PostMapping("/logout")
    @ResponseBody
    public ApiRestResponse logout(HttpSession session){
        session.removeAttribute(Constant.USER);
        return ApiRestResponse.success();
    }


    /**
     * 管理员登录
     * @param userName
     * @param password
     * @param session
     * @return
     * @throws LearnMallException
     */
    @PostMapping("/adminLogin")
    @ResponseBody
    public ApiRestResponse adminLogin(@RequestParam("userName") String userName,
                                 @RequestParam("password") String password, HttpSession session) throws LearnMallException {
        //用户名不能为空
        if (StringUtils.isEmpty(userName)) {
            return ApiRestResponse.error(LearnMallExceptionEnum.NEED_USER_NAME);
        }
        //密码不能为空
        if (StringUtils.isEmpty(password)) {
            return ApiRestResponse.error(LearnMallExceptionEnum.NEED_PASSWORD);
        }
        //进行登录逻辑
        User user = userService.login(userName,password);
        if(user.getRole().equals(2)){
            //返回用户信息时不展示密码信息
            user.setPassword(null);
            //将用户信息存入session中
            session.setAttribute(Constant.USER,user);
            return ApiRestResponse.success(user);
        }else{
            return ApiRestResponse.error(LearnMallExceptionEnum.NEED_ADMIN);
        }
    }

}

 二、统一异常处理

 1、编写自定义异常

package com.learn.mall.exception;

/**
 * 统一异常
 */
public class LearnMallException extends Exception {
    private final Integer code;
    private final String msg;


    public LearnMallException(Integer code,String msg){
        this.code = code;
        this.msg = msg;
    }

    public LearnMallException(LearnMallExceptionEnum exceptionEnum){
        this(exceptionEnum.getCode(),exceptionEnum.getMsg());
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }
}

 2、编写自定义异常枚举类,统一管理各种异常信息

package com.learn.mall.exception;

/**
 * 异常枚举
 */
public enum LearnMallExceptionEnum {
    NEED_USER_NAME(10001,"用户名不能为空!"),
    NEED_PASSWORD(10002,"密码不能为空!"),
    PASSWORD_TOO_SHORT(10003,"密码长度不能小于8位!"),
    USERNAME_EXISTED(10004,"用户名已存在!"),
    INSERT_FAILED(10005,"数据插入失败!"),
    LOGIN_ERROR(10006,"用户名或密码不正确!"),
    NEED_LOGIN(10007,"用户未登录!"),
    NEED_ADMIN(10008,"无管理员权限!"),
    UPDATE_FAILED(10008,"更新用户信息失败!"),
    SYSTEM_ERROR(20001,"系统异常!");

    //异常码
    Integer code;
    //异常信息
    String msg;

    LearnMallExceptionEnum(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "LearnMallExceptionEnum{" +
                "code=" + code +
                ", msg=‘" + msg + ‘\‘‘ +
                ‘}‘;
    }
}

3、自定义异常的使用

package com.learn.mall.service.impl;

import com.learn.mall.exception.LearnMallException;
import com.learn.mall.exception.LearnMallExceptionEnum;
import com.learn.mall.model.dao.UserMapper;
import com.learn.mall.model.pojo.User;
import com.learn.mall.service.UserService;
import com.learn.mall.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User getUser() {
        return userMapper.selectByPrimaryKey(1);
    }

    @Override
    public void register(String username, String password) throws LearnMallException {
        //查询用户是否存在,不允许重名
        User result = userMapper.selectByName(username);
        if (result != null) {
            throw new LearnMallException(LearnMallExceptionEnum.USERNAME_EXISTED);
        }
        //用户不存在,执行插入操作
        User user = new User();
        user.setUsername(username);
        //对密码进行MD5加密并加盐
        try {
            user.setPassword(MD5Utils.getMD5Str(password));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        int count = userMapper.insertSelective(user);
        if(count == 0){
            throw new LearnMallException(LearnMallExceptionEnum.INSERT_FAILED);
        }
    }

    @Override
    public User login(String username, String password) throws LearnMallException {
        //将密码转换为MD5形式的字符串,再去数据库中查询
        String MD5Password = null;
        try {
            MD5Password = MD5Utils.getMD5Str(password);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        User user = userMapper.selectLogin(username,MD5Password);
        //用户不存在则抛出异常
        if(user == null){
            throw new LearnMallException(LearnMallExceptionEnum.LOGIN_ERROR);
        }
        return user;
    }

    @Override
    public void updateUserInfo(User user) throws LearnMallException {
        int updateCount = userMapper.updateByPrimaryKeySelective(user);
        if(updateCount > 1){
            throw new LearnMallException(LearnMallExceptionEnum.UPDATE_FAILED);
        }
    }
}

4、编写异常拦截器,统一处理异常

package com.learn.mall.exception;

import com.learn.mall.common.ApiRestResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 处理统一异常的handler
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 拦截系统异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object handleException(Exception e) {
        log.error("Default Exception:", e);
        return ApiRestResponse.error(LearnMallExceptionEnum.SYSTEM_ERROR);
    }

    /**
     * 拦截自定义业务异常
     * @param e
     * @return
     */
    @ExceptionHandler(LearnMallException.class)
    @ResponseBody
    public Object handleLearnMallException(LearnMallException e) {
        log.error("LearnMallException Exception:", e);
        return ApiRestResponse.error(e.getCode(),e.getMsg());
    }
}

 

Spring Boot项目开发(三)——统一响应对象、统一异常处理

标签:catch   autowired   try   nbsp   get   信息   OLE   user   move   

原文地址:https://www.cnblogs.com/michealyang/p/14089769.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!