赞
踩
我们平时的WEB 开发Controller中会经常用到通用返回类,及全局异常的捕获和处理,及自定义业务异常的情况,接下来我给大家梳理一下配置步骤
package com.dechnic.transfer.common;/**
* @className: AjaxResult
* @author: houqd
* @date: 2022/11/2
**/
import java.io.Serializable;
import java.util.HashMap;
/**
*@description: 通用返回结果类
*@author:houqd
*@time: 2022/11/2 14:26
*
*/
public class AjaxResult extends HashMap<String,Object> implements Serializable {
private static final String REQ_TAG = "success";//请求状态
/*状态码*/
private static final String CODE_TAG = "code";
/*返回内容*/
private static final String MSG_TAG = "message";
/*数据对象*/
private static final String DATA_TAG = "data";
public AjaxResult (boolean success,String code,String msg,Object data){
super.put(REQ_TAG,success);
super.put(CODE_TAG,code);
super.put(MSG_TAG,msg);
super.put(DATA_TAG, data);
}
public AjaxResult (String code,String msg,Object data){
super.put(REQ_TAG,true);
super.put(CODE_TAG,code);
super.put(MSG_TAG,msg);
super.put(DATA_TAG, data);
}
public AjaxResult (String code,String msg){
super.put(REQ_TAG,true);
super.put(CODE_TAG,code);
super.put(MSG_TAG,msg);
}
public AjaxResult() {
}
public static AjaxResult success(String msg,Object data){
return new AjaxResult(StatusCode.SUCCESS.getCode(), msg,data);
}
public static AjaxResult success(Object data){
return new AjaxResult(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getDesc(),data);
}
public static AjaxResult error(boolean success,String code ,String msg){
return new AjaxResult(success,code, msg,null);
}
public static AjaxResult error(String code ,String msg){
return new AjaxResult(code, msg,null);
}
public static AjaxResult error(String msg){
return new AjaxResult(StatusCode.ERROR.getCode(), msg,null);
}
public static AjaxResult error(){
return new AjaxResult(StatusCode.ERROR.getCode(), StatusCode.ERROR.getDesc(),null);
}
public static AjaxResult serviceInnerError(){
return new AjaxResult(StatusCode.SERVICE_ERROR.getCode(), StatusCode.SERVICE_ERROR.getDesc(),null);
}
@Override
public Object put(String key, Object value) {
super.put(key, value);
return this;
}
}
package com.dechnic.transfer.common;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
/**
* @className: StatusCode
* @author: houqd
* @date: 2022/11/2
**/
@Getter
@NoArgsConstructor
@AllArgsConstructor
public enum StatusCode {
SUCCESS("1","成功"),ERROR("0","失败"),NO_LOGIN("401","未登录"),SERVICE_ERROR("500","服务器错误");
private String code;
private String desc;
}
GlobalExceptionHandler
package com.dechnic.transfer.exception;/**
* @className: GlobalExceptionHandler
* @author: houqd
* @date: 2022/11/2
**/
import com.dechnic.transfer.common.AjaxResult;
import com.dechnic.transfer.common.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
/**
*@description:
*@author:houqd
*@time: 2022/11/2 15:34
*
*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(NotLoginException.class)
public AjaxResult handlerNotLoginException(NotLoginException e, HttpServletRequest request){
String requestURI =request.getRequestURI();
log.info("请求地址:'{}',未登陆",requestURI);
return AjaxResult.error(false,e.getCode(),e.getMsg());
}
@ExceptionHandler(BusinessException.class)
public AjaxResult handlerBusinessException(BusinessException e, HttpServletRequest request){
String requestURI =request.getRequestURI();
log.info("请求地址:'{}',业务异常:{}",requestURI,e.getMessage());
return AjaxResult.error(false,e.getCode(),e.getMsg());
}
@ExceptionHandler(Exception.class)
public AjaxResult handlerBusinessException(Exception e, HttpServletRequest request){
String requestURI =request.getRequestURI();
log.info("请求地址:'{}',服务器内部异常:{}",requestURI,e.getMessage());
e.printStackTrace();
return AjaxResult.error(false,StatusCode.SERVICE_ERROR.getCode(), StatusCode.SERVICE_ERROR.getDesc());
}
}
BusinessException 业务异常
package com.dechnic.transfer.exception;/**
* @className: BusinessException
* @author: houqd
* @date: 2022/11/2
**/
import lombok.Data;
/**
*@description:
*@author:houqd
*@time: 2022/11/2 15:20
*
*/
@Data
public class BusinessException extends RuntimeException{
private String code;
private String msg;
public BusinessException(String code, String msg) {
this.code = code;
this.msg = msg;
}
@Override
public String getMessage() {
return msg;
}
}
NotLoginException
package com.dechnic.transfer.exception;/**
* @className: NotLoginException
* @author: houqd
* @date: 2022/11/2
**/
import com.dechnic.transfer.common.StatusCode;
/**
*@description:
*@author:houqd
*@time: 2022/11/2 15:37
*
*/
public class NotLoginException extends BusinessException{
public NotLoginException() {
super(StatusCode.NO_LOGIN.getCode(), StatusCode.NO_LOGIN.getDesc());
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。