springboot3和vue2的学习2

其他后端代码

GlobalException.java

设置错误返回结果

package com.example.demo1215day1215.exception;

import com.example.demo1215day1215.commons.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 GlobalException {
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public Result serviceException(ServiceException e) {
        return Result.error(e.getCode(),e.getMessage());
    }
}

ServiceException.java

package com.example.demo1215day1215.exception;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class ServiceException extends RuntimeException {
    private String code;
    public ServiceException(String msg) {
        super(msg);
        this.code = "500";
    }
    public ServiceException(String code,String msg) {
        super(msg);
        this.code = code;
    }

}
posted @ 2024-12-18 22:44  haoyinuo  阅读(13)  评论(0)    收藏  举报