SpringBoot项目中对于前端数据响应

文章介绍了如何在Java中创建自定义异常类`ServiceException`继承自`RuntimeException`,并设计了一个响应结果类`ResponseResult`用于封装前端返回的数据。此外,还展示了一个`BaseController`类,它使用`@ControllerAdvice`注解进行全局异常处理,针对不同类型的异常返回不同的错误信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.在业务成层中写一个ex包,后再写ServiceException的类去继承 RuntimeException

package com.cy.service.ex;

public class ServiceException extends  RuntimeException{
    public ServiceException() {
        super();


    }

    public ServiceException(String message) {
        super(message);
    }

    public ServiceException(String message, Throwable cause) {
        super(message, cause);
    }

    public ServiceException(Throwable cause) {
        super(cause);
    }

    protected ServiceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}

而后可以在写多个异常类去继承ServiceException

2.写一个util工具包进行前端数据的响应

package com.cy.util;

import java.io.Serializable;

/**
 * 响应结果类
 * @param <E> 响应数据的类型
 */
public class ResponseResult<E> implements Serializable {
    /** 操作成功的状态码 */
    public static final int OK = 200;

    /** 状态码 */
    private Integer state;
    /** 状态描述信息 */
    private String message;
    /** 数据 */
    private E data;

    public ResponseResult() {
    }

    public ResponseResult(Integer state, String message) {
        this.state = state;
        this.message = message;
    }

    public ResponseResult(Integer state, String message, E data) {
        this.state = state;
        this.message = message;
        this.data = data;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public E getData() {
        return data;
    }

    public void setData(E data) {
        this.data = data;
    }


    public static <E> ResponseResult<E> getResponseResult() {
        return new ResponseResult<>(OK, null, null);
    }

    public static <E> ResponseResult<E> getResponseResult(Integer state) {
        return new ResponseResult<>(state, null, null);
    }

    public static <E> ResponseResult<E> getResponseResult(String message) {
        return new ResponseResult<>(OK, message, null);
    }

    public static <E> ResponseResult<E> getResponseResult(E data) {
        return new ResponseResult<>(OK, null, data);
    }

    public static <E> ResponseResult<E> getResponseResult(Integer state, String message) {
        return new ResponseResult<>(state, message, null);
    }

    public static <E> ResponseResult<E> getResponseResult(String message, E data) {
        return new ResponseResult<>(OK, message, data);
    }
}

3.写一个异常处理类

package com.cy.controller;

import com.cy.service.ex.InsertException;
import com.cy.service.ex.ServiceException;
import com.cy.service.ex.UsernameDuplicateException;
import com.cy.util.ResponseResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/*
* 控制层的基类
* */
@ControllerAdvice
public class BaseController  extends zhuang{
    /* 统一异常处理 */
    @ExceptionHandler(ServiceException.class)//监控当前类是否产生异常
    public ResponseResult<Void> handleException(Throwable e){
       ResponseResult<Void> responseResult=null;
       if (e instanceof UsernameDuplicateException){
            responseResult = ResponseResult.getResponseResult(zhuan1, e.getMessage());
       }
        if (e instanceof InsertException){
            responseResult = ResponseResult.getResponseResult(5000, e.getMessage());
        }

        return responseResult;
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值