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;
}
}

浙公网安备 33010602011771号