毕业设计Day12--登录功能改进

springboot自定义异常

在Exception中自定义异常

(ServiceException,继承于RuntimeException)

package com.ww.car.exception;

import lombok.Getter;

@Getter
public class ServiceException extends RuntimeException{

    private String code;

    public ServiceException(String code, String msg){
        super(msg);
        this.code = code;
    }

}

进行异常捕获

在GlobalExceptionHandler中捕获异常,再将其包装成一个Result对象进行返回

package com.ww.car.exception;


import com.ww.car.common.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public Result handle(ServiceException se){
        return Result.error(se.getCode(),se.getMessage());
    }
}

Result定义

package com.ww.car.common;

/**
 * 接口统一返回包装类
 */

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
    private String code;
    private String msg;
    private Object data;
    public static Result success(Object data){
        return new Result(Constants.CODE_200,"",data);
    }
    public static Result success(){
        return new Result(Constants.CODE_200,"",null);
    }
    public static Result error(String code,String msg){
        return new Result(Constants.CODE_200,msg,null);
    }
    public static Result error(){
        return new Result(Constants.CODE_500,"系统错误",null);
    }
}

UserDTO定义

package com.ww.car.controller.dto;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ww.car.common.Constants;
import com.ww.car.common.Result;
import com.ww.car.entity.User;
import lombok.Data;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 接收前端用户登录请求的数据
 */

@Data

public class UserDTO {
    private String username;
    private String password;
    private String nickname;
    private String avatarUrl;




}

Constants接口定义

package com.ww.car.common;

public interface Constants {
    String CODE_200 = "200";//成功
    String CODE_500 = "500";//系统错误
    String CODE_401 = "401";//权限不足
    String CODE_400 = "400";//参数错误
    String CODE_600 = "600";//其他业务异常

}

UserService中的登录

@Override
    public UserDTO login(UserDTO userDTO){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",userDTO.getUsername());
        queryWrapper.eq("password",userDTO.getPassword());
        User one;
        try{
            one = getOne(queryWrapper);
        }catch (Exception e){
            LOG.error(e);
            throw new ServiceException(Constants.CODE_500,"系统错误");
        }
        if(one != null){
            BeanUtils.copyProperties(one,userDTO);
            return userDTO;
        }else {
            throw new ServiceException(Constants.CODE_600,"用户名或密码错误");
        }

    }

UserController中的登录

 @PostMapping("/login")
        public Result login(@RequestBody UserDTO userDTO) {
            String username = userDTO.getUsername();
            String password = userDTO.getPassword();
            if(StrUtil.isBlank(username) || StrUtil.isBlank(password)){
                return Result.error(Constants.CODE_400,"参数错误");
            }
            UserDTO dto = userService.login(userDTO);
            return Result.success(dto);
        }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值