SpringBoot统一数据返回模板---全局异常处理

一、统一数据返回模板

创建一个消息类Message.java,代码如下

package com.ws.demo.vo;

import com.ws.demo.enumClass.CallType;

public class Message {
     

    //调用结果类型
    private CallType callType;
    //消息
    private String msg;
    //数据
    private Object data;
    //状态码
    private Integer status;
    //异常链接
    private String url;

	/**
	 * 此处省略  get set toString
	 **/
    
    public Message() {
     
    }

    /**
     * 错误返回
     * @param callType
     * @param msg
     */
    public Message(CallType callType, String msg, Integer status, String url) {
     
        this.callType = callType;
        this.msg = msg;
        this.status = status;
        this.url = url;
    }

    /**
     * 成功返回
     * @param callType
     * @param msg
     * @param data
     */
    public Message(CallType callType, String msg, Object data, Integer status) {
     
        this.callType = callType;
        this.msg = msg;
        this.data = data;
        this.status = status;
    }

    /**
     * 返回成功消息
     * @param data  返回数据
     * @return
     */
    public static Message success(Object data){
     
        return new Message(CallType.success,"操作成功!",data,0);
    }

    /**
     * 返回失败消息
     * @param msg
     * @return
     */
    public static Message unsuccess(String msg,String url){
     
        return new Message(CallType.error,msg,-1,url);
    }
}

创建调用结果状态枚举类CallType.java,代码如下

package com.ws.demo.enumClass;

public enum CallType {
     
    success,
    warn,
    error;
}

来测试一下吧

@RestController
public class TestController {
     

    @RequestMapping("testMessage")
    public Message testMessage(){
     
        //SocketMsg为一个实体类
        SocketMsg sm = null;
        try {
     
            sm = null;
            sm = new SocketMsg();
            sm.setFromUser("LFQ");
            sm.setMsg("我来了!");
            sm.setToUser("YXX");
            sm.setType(1);
            //此处模拟代码出现异常
            //int flag = 1 / 0;
        } catch (Exception e) {
     
        	//失败状态下调用unsuccess方法
            return Message.unsuccess(e.getMessage(),request.getRequestURI());
        }
        //成功状态下调用success方法
        return Message.success(sm);
    }
}

效果如下:
SpringBoot统一数据返回模板---全局异常处理_第1张图片

二、全局异常处理

1、定义一个异常类HospitalException.java,继承Exception

public class HospitalException extends Exception{
     
    public HospitalException(String message) {
     
        super(message);
    }
}

2、创建自定义异常类型枚举类ExceptionType.java

public enum ExceptionType {
     
    SqlException("数据库异常!"),
    ServiceException("算法服务调用失败!"),
    NullException("未知异常!");
    
    private String msg;
    
    ExceptionType(String msg) {
     
        this.msg = msg;
    }
    
    public String getMsg() {
     return msg;}
    public void setMsg(String msg) {
     this.msg = msg;}
}

3、配置全局异常处理器,处理HospitalException

@Configuration
@ControllerAdvice
public class ExceptionController {
     

    /**
     * //异常处理器,处理HospitalException异常
     * @param request
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = HospitalException.class)
    public ResponseEntity<?> hanlerException(HttpServletRequest request, HospitalException e){
     
    	//返回自定义消息
        Message error = Message.unsuccess(e.getMessage(),request.getRequestURI());
        return new ResponseEntity<>(error, HttpStatus.OK);
    }
}

4、来测试一下吧

@RestController
public class TestController {
     

    @RequestMapping("testMessage")
    public Message testMessage(HttpServletRequest request)throws HospitalException{
     

        SocketMsg sm = null;
        try {
     
            sm = null;
            sm = new SocketMsg();
            sm.setFromUser("LFQ");
            sm.setMsg("我来了!");
            sm.setToUser("YXX");
            sm.setType(1);
            //模拟出现异常
            int flag = 1 / 0;
        } catch (Exception e) {
     
            ExceptionType et = ExceptionType.NullException;
            //这里消息也可以是系统异常消息
            throw new HospitalException(et.getMsg());
        }
        return Message.success(sm);
    }
}

效果如下
SpringBoot统一数据返回模板---全局异常处理_第2张图片

你可能感兴趣的:(java后端)