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

一、统一响应对象

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

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());
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot 是一种快速构建企业级应用的框架。在建立个人博客系统时,它可以帮助开发人员快速构建博客系统的后端架构,并使用其内置的自动配置功能为博客系统提供各种功能,如数据持久化、安全认证、文件上传和下载等。 使用 Spring Boot 开发个人博客系统可以解决以下问题: 1. 快速构建后端架构:Spring Boot 内置了许多常用的框架和库,可以帮助开发人员快速构建出博客系统的后端架构。 2. 自动配置功能:Spring Boot 可以自动配置许多常用的功能,如数据库连接、安全认证、文件上传下载等,使开发人员可以专注于开发业务逻辑。 3. 简化部署流程:Spring Boot 可以使用内置的 tomcat 服务器进行部署,简化了部署流程。 4. 提供企业级应用所需的功能:Spring Boot 提供了许多企业级应用所需的功能,如安全认证、数据持久化等,可以使个人博客系统更加完善。 ### 回答2: Spring Boot 个人博客系统是一款基于Spring Boot框架开发的博客系统,它能够解决以下问题: 1. 快速开发Spring Boot框架提供了许多开箱即用的功能和组件,极大地简化了开发流程。其中包括自动配置、内嵌服务器、自动项目构建等,可以快速搭建一个功能完善的博客系统。 2. 简化部署:Spring Boot使用嵌入式服务器,如Tomcat或Jetty,可以将应用程序打包成一个可执行的JAR文件,只需执行一条命令即可启动应用程序,无需额外部署和配置服务器,极大地简化了部署流程。 3. 高度可定制:Spring Boot提供了丰富的配置选项和扩展点,可以根据个人需求进行灵活的定制。可通过配置文件进行自定义配置,也可以通过扩展Spring Boot的自动配置机制来引入额外的功能。 4. 高效性能:Spring Boot集成了很多优化的组件,如Spring MVC、Spring Data、Spring Security等,能够提供高性能的Web服务。此外,Spring Boot还支持异步处理、缓存机制等,可以进一步提升系统的响应速度和并发能力。 5. 微服务支持:Spring Boot天生支持构建微服务架构,可以将一个大型的博客系统拆分为多个小型的服务,每个服务专注于特定的功能,提高了系统的可维护性和可扩展性。 6. 生态系统丰富:Spring BootSpring生态系统的一部分,可以与其他Spring项目无缝集成,如Spring Cloud、Spring Security等。同时,Spring Boot还支持各种第方库和工具的集成,如数据库、缓存、消息队列等。 综上所述,Spring Boot个人博客系统不仅能够提供快速开发和部署的能力,还能够提供高度定制性、高效性能、微服务支持以及丰富的生态系统,为个人博客的开发者提供了一个可靠和便捷的解决方案。 ### 回答3: Spring Boot个人博客系统是一种基于Spring Boot框架开发的博客系统,它能够解决以下几个问题: 1. 快速开发Spring Boot个人博客系统采用了Spring Boot框架,这个框架提供了很多开箱即用的特性,如自动配置、快速构建等,极大地减少了开发者的开发时间和精力,使得开发者能够更专注于业务逻辑的实现,从而实现快速开发。 2. 简化配置:Spring Boot个人博客系统采用了约定优于配置的原则,大部分的配置都可以通过少量的配置文件完成,大大简化了系统的配置工作。同时,Spring Boot还有一个在线配置工具——Spring Initializr,可以在线生成项目的初始配置,进一步减轻了配置的负担。 3. 整合丰富的开源组件:Spring Boot个人博客系统内置了许多常用的开源组件,如Thymeleaf模板引擎、Spring Data JPA、Spring Security等,这些组件都经过了充分的测试和验证,能够提供稳定、高效的功能实现。同时,Spring Boot还提供了自动配置功能,可以自动根据项目的依赖关系自动配置相关的组件,大大简化了整合的工作。 4. 响应式设计:Spring Boot个人博客系统支持响应式设计,可以很方便地适配不同的终端设备,如PC、手机、平板等,提供更好的用户体验。 总之,Spring Boot个人博客系统通过快速开发、简化配置、整合丰富的开源组件和响应式设计等特性,能够解决开发者在开发个人博客系统时遇到的瓶颈,提高开发效率,降低开发成本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值