项目搭建工具及版本:
eclipse / jdk1.8 / springboot2.5.0
实现功能:
主要实现2个点,自定义的异常捕获返回给请求方,正常请求的统一返回处理。
1.主要功能类说明
错误码枚举类、响应返回实体类、响应返回实体工具类、自定义异常、自定义异常拦截器配置类
2.源代码
package com.bbnet.common.exception;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 常见的错误枚举类
*
*
* @author sgc
* @since 2021-08-27
*/
@Getter
@AllArgsConstructor
public enum ErrorEnum {
RESP_CODE_SUCC("0","请求成功"),
RESP_CODE_FAIL("1","请求失败"),
RESP_CODE_NO_TOKEN("-1","TOKEN为空"),
RESP_CODE_TIMEOUT("-2","会话超时或非法请求");
/**
* 错误码
*/
private String code;
/**
* 提示信息
*/
private String msg;
}
package com.bbnet.common.exception;
import lombok.Data;
/**
* 响应对象
*
* @param <T>
*
* @author sgc
* @since 2021-08-27
*/
@Data
public class ResponseResult<T> {
/**
* 状态码
*/
private String code;
/**
* 提示信息
*/
private String msg;
/**
* 请求成功时返回的对象
*/
private T data;
}
package com.bbnet.common.exception;
/**
* 响应对象工具类
*
*
* @author sgc
* @since 2021-08-27
*/
public class ResponseUtils {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static ResponseResult success(Object obj){
ResponseResult res = new ResponseResult();
res.setCode(ErrorEnum.RESP_CODE_SUCC.getCode());
res.setData(obj);
res.setMsg(ErrorEnum.RESP_CODE_SUCC.getMsg());
return res;
}
@SuppressWarnings("rawtypes")
public static ResponseResult success(){
return success(null);
}
@SuppressWarnings("rawtypes")
public static ResponseResult error(String code, String msg){
ResponseResult res = new ResponseResult();
res.setCode(code);
res.setMsg(msg);
return res;
}
}
package com.bbnet.common.exception;
import lombok.Getter;
/**
* 自定义异常
*
*
* @author sgc
* @since 2021-08-27
*/
@Getter
public class ServiceException extends RuntimeException{
private static final long serialVersionUID = 7133651385712198609L;
private String code;
/**
* 使用已有的错误类型
* @param type 枚举类中的错误类型
*/
public ServiceException(ErrorEnum type){
super(type.getMsg());
this.code = type.getCode();
}
/**
* 自定义错误类型
* @param code 自定义的错误码
* @param msg 自定义的错误提示
*/
public ServiceException(String code, String msg){
super(msg);
this.code = code;
}
}
package com.bbnet.common.exception;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bbnet.common.encrypt.annotation.Encrypt;
/**
* 异常处理类
*
*
* @author sgc
* @since 2021-08-27
*/
@ControllerAdvice
public class ServiceExceptionHandler {
/**
* <pre>
* ExceptionHandler相当于controller的@RequestMapping
* 如果抛出的的是ServiceException,则调用该方法
* </pre>
*
* @param se 业务异常
* @return
*/
@SuppressWarnings("rawtypes")
@ExceptionHandler(ServiceException.class)
@ResponseBody
@Encrypt
public ResponseResult handle(ServiceException se){
return ResponseUtils.error(se.getCode(), se.getMessage());
}
}
3.测试
如下面的部分代码,如有异常可以直接抛出,如正常返回直接返回ResponseUtils.success()或者ResponseUtils.success(Obj)
//code...
@SuppressWarnings("rawtypes")
@ApiOperation(value="删除token接口", notes="token时效情况下删除token,即类似于退出登录")
@PostMapping("/removeToken")
public ResponseResult removeToken(@RequestBody Token token) {
//参数校验
if(oConvertUtils.isEmpty(token.getAccount())) {
throw new ServiceException(ErrorEnum.RESP_CODE_FAIL.getCode(), "账号或用户名为空");
}
//account的加密结果
String accountEnc = AES.aesEncryptBase64(token.getAccount());
//bbnet:user_prefix_token:002604_MnJMUnY1TWNhVmZsMXlLQjNkajR6QT09
String key = ServiceConstants.TOKEN_PREFIX+token.getAccount()+"_"+accountEnc;
log.info("Token的key为:{}", key);
//如果已经存在token直接返回
if(redisService.hasKey(key)) {
redisService.del(key);
}
return ResponseUtils.success();
}
//code...
package com.bbnet.common.base.token;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Setter
@Getter
@ToString
@ApiModel(value="TOKEN报文实体")
public class Token {
@ApiModelProperty(value = "账号/用户名", dataType = "String")
String account;
@ApiModelProperty(value = "口令/密码", dataType = "String")
String password;
}
比如以下请求示例截图: