自定义结果
R对象
package com.gdpu.util;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author mojiazhu
* @date 2022/12/9 20:57
*/
@Data
@NoArgsConstructor
@JsonInclude(Include.NON_NULL)
public class R<T> {
private String code;
private String message;
private T data;
private Object[] msgParams;
//异常返回
public R(String code, String message) {
this.code = code;
this.message = message;
}
//调用成功返回
public R(String code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
}
//异常返回,带异常参数
public R(String code, String message, T data, Object[] msgParams) {
this.code = code;
this.message = message;
this.data = data;
this.msgParams = msgParams;
}
}
统一拦截结果
package com.gdpu.advice;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
/**
* @author mojiazhu
* @date 2022/12/9 21:43
*/
//不启用的全局统一返回对象
@RestControllerAdvice(basePackages = "com.gdpu.controller")
public class ResponseAdvice implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return true;
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
ServerHttpResponse response) {
return body;
}
}
我这里没有启用,要使用的话,就是在beforeBodyWrite这里将返回结果变为R对象,
即->new R<>("000", "接口调用成功",body)
自定义异常
异常对象
package com.gdpu.exception;
import com.gdpu.face.IBusinessError;
import lombok.Getter;
/**
* 自定义异常
*
* @author mojiazhu
* @date 2022/12/9 19:51
*/
@Getter
public class BusinessException extends RuntimeException {
private final IBusinessError businessError;
private Object[] msgParams;
/**
* 异常能够在resources下找到
*/
public BusinessException(IBusinessError businessError) {
super(businessError.getCode());
this.businessError = businessError;
}
/**
* 此类异常为自定义消息异常,请将异常code设定为999
*/
public BusinessException(IBusinessError businessError, String debugMessage) {
super(debugMessage);
this.businessError = businessError;
}
/**
* 此类异常为自定义消息异常,请将异常code设定为999 可提供异常对象
*/
private BusinessException(IBusinessError businessError, Object[] msgParams, String debugMessage) {
super(debugMessage);
this.businessError = businessError;
this.msgParams = msgParams;
}
/**
* 此类异常为自定义消息异常,请将异常code设定为999 可提供异常对象,平且提供父类异常
*/
private BusinessException(IBusinessError businessError, Object[] msgParams, String debugMessage, Throwable t) {
super(debugMessage, t);
this.businessError = businessError;
this.msgParams = msgParams;
}
}
package com.gdpu.face;
/**
* @author mojiazhu
* @date 2022/12/9 19:57
*/
public interface IBusinessError {
String getCode();
}
package com.gdpu.enumeration;
import com.gdpu.face.IBusinessError;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* @author mojiazhu
* @date 2022/12/9 19:56
*/
@Getter
@AllArgsConstructor
public enum BusinessError implements IBusinessError {
CUSTOM_EXCEPTION("999"),
SYSTEM_ERROR("001"),
ACCOUNT_FAILED_ERROR("002"),
REQUEST_PARAM_ERROR("003");
private String code;
}
统一拦截异常
package com.gdpu.advice;
import com.gdpu.exception.BusinessException;
import com.gdpu.exception.TokenException;
import com.gdpu.enumeration.BusinessError;
import com.gdpu.util.R;
import java.io.UnsupportedEncodingException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.ResourceBundle;
import com.mysql.cj.log.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* @author mojiazhu
* @date 2022/12/9 20:11
*/
@RestControllerAdvice(basePackages = "com.gdpu.controller")
@Slf4j
public class MyExceptionAdvice {
/**
* 读取resource下的异常文件
*/
private static final ResourceBundle bundle = ResourceBundle.getBundle("error.error_message");
/**
* 捕获自定义异常
*/
@ExceptionHandler({BusinessException.class})
public R<?> handleBusinessException(BusinessException e) throws UnsupportedEncodingException {
log.info("\n发生异常,异常码:【{}】,异常信息:【{}】", e.getBusinessError().getCode(), e.getMessage());
if ("999".equals(e.getBusinessError().getCode())) {
return new R<>("999", e.getMessage());
}
String message =
new String(bundle.getString(e.getBusinessError().getCode()).getBytes("ISO-8859-1"), "UTF-8");
return new R<>(e.getBusinessError().getCode(), message);
}
/**
* 捕获参数校验失败异常 {@code @RequestBody @RequestParam @PathVariable}
*/
@ExceptionHandler({MethodArgumentNotValidException.class, SQLIntegrityConstraintViolationException.class})
public R<?> handleValidException(MethodArgumentNotValidException e) {
log.info("\n发生异常,异常码:【777】,异常信息:【参数校验异常】");
return new R<>(BusinessError.REQUEST_PARAM_ERROR.getCode(), "参数校验异常");
}
/**
* 捕获token验证失败异常
*/
@ExceptionHandler({TokenException.class})
public R<?> handleTokenException(TokenException e) {
log.info("\n发生异常,异常码:【888】,异常信息:【Token验证失败】");
return new R<>("888", "Token验证失败");
}
}
异常文件
error.error_message
001=系统异常。
002=账号验证失败。
003=请求超时。
使用
throw new BusinessException(BusinessError.CUSTOM_EXCEPTION, "已经评论,请勿重复操作");