文章目录
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);
}