parent
34a3d04f1d
commit
9de258af0d
@ -0,0 +1,42 @@
|
|||||||
|
package com.java3y.austin.web.advice;
|
||||||
|
|
||||||
|
import com.java3y.austin.common.vo.BasicResultVO;
|
||||||
|
import com.java3y.austin.web.annotation.AustinResult;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.springframework.core.MethodParameter;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.server.ServerHttpRequest;
|
||||||
|
import org.springframework.http.server.ServerHttpResponse;
|
||||||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author kl
|
||||||
|
* @version 1.0.0
|
||||||
|
* @description 统一返回结构
|
||||||
|
* @date 2023/2/9 19:00
|
||||||
|
*/
|
||||||
|
@ControllerAdvice(basePackages = "com.java3y.austin.web.controller")
|
||||||
|
public class AustinResponseBodyAdvice implements ResponseBodyAdvice<Object> {
|
||||||
|
|
||||||
|
private static final String RETURN_CLASS = "BasicResultVO";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supports(MethodParameter methodParameter, Class aClass) {
|
||||||
|
return methodParameter.getContainingClass().isAnnotationPresent(AustinResult.class) || methodParameter.hasMethodAnnotation(AustinResult.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object beforeBodyWrite(Object data, MethodParameter methodParameter, MediaType mediaType, Class aClass,
|
||||||
|
ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
|
||||||
|
if (Objects.nonNull(data) && Objects.nonNull(data.getClass())) {
|
||||||
|
String simpleName = data.getClass().getSimpleName();
|
||||||
|
if (RETURN_CLASS.equalsIgnoreCase(simpleName)) {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return BasicResultVO.success(data);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.java3y.austin.web.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author kl
|
||||||
|
* @version 1.0.0
|
||||||
|
* @description 统一返回注解
|
||||||
|
* @date 2023/2/9 19:00
|
||||||
|
*/
|
||||||
|
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface AustinResult {
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.java3y.austin.web.exception;
|
||||||
|
|
||||||
|
import com.java3y.austin.common.enums.RespStatusEnum;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author kl
|
||||||
|
* @version 1.0.0
|
||||||
|
* @description 通用异常
|
||||||
|
* @date 2023/2/9 19:00
|
||||||
|
*/
|
||||||
|
public class CommonException extends RuntimeException {
|
||||||
|
private String code = RespStatusEnum.ERROR_400.getCode();
|
||||||
|
|
||||||
|
public CommonException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommonException(RespStatusEnum respStatusEnum) {
|
||||||
|
super(respStatusEnum.getMsg());
|
||||||
|
this.code = respStatusEnum.getCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommonException(String code, String message) {
|
||||||
|
super(message);
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommonException(String message, Exception e) {
|
||||||
|
super(message, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommonException(String code, String message, Exception e) {
|
||||||
|
super(message, e);
|
||||||
|
this.code = code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCode() {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.java3y.austin.web.exception;
|
||||||
|
|
||||||
|
import com.java3y.austin.common.enums.RespStatusEnum;
|
||||||
|
import com.java3y.austin.common.vo.BasicResultVO;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author kl
|
||||||
|
* @version 1.0.0
|
||||||
|
* @description 拦截异常统一返回
|
||||||
|
* @date 2023/2/9 19:03
|
||||||
|
*/
|
||||||
|
@ControllerAdvice
|
||||||
|
@ResponseBody
|
||||||
|
public class ExceptionHandlerAdvice {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(ExceptionHandlerAdvice.class);
|
||||||
|
|
||||||
|
public ExceptionHandlerAdvice() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler({Exception.class})
|
||||||
|
@ResponseStatus(HttpStatus.OK)
|
||||||
|
public BasicResultVO exceptionResponse(Exception e) {
|
||||||
|
StringWriter sw = new StringWriter();
|
||||||
|
PrintWriter pw = new PrintWriter(sw);
|
||||||
|
e.printStackTrace(pw);
|
||||||
|
BasicResultVO result = BasicResultVO.fail(RespStatusEnum.ERROR_500,"\r\n" + sw + "\r\n");
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler({CommonException.class})
|
||||||
|
@ResponseStatus(HttpStatus.OK)
|
||||||
|
public BasicResultVO commonResponse(CommonException ce) {
|
||||||
|
BasicResultVO result = BasicResultVO.fail(RespStatusEnum.ERROR_400, ce.getMessage());
|
||||||
|
log.error(ce.getMessage(), ce);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in new issue