parent
14c86578fb
commit
17a6939593
@ -0,0 +1,33 @@
|
||||
package com.supervision.config;
|
||||
|
||||
import com.supervision.common.domain.R;
|
||||
import com.supervision.common.enums.ResultStatusEnum;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
|
||||
/**
|
||||
* 统一返回
|
||||
*
|
||||
* @author wb
|
||||
* @date 2022/3/10 13:24
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class ExceptionResponseConfig {
|
||||
|
||||
/**
|
||||
* 添加手动校验参数的异常处理
|
||||
* @param exception 参数验证异常
|
||||
* @return 通用返回值
|
||||
*/
|
||||
@ExceptionHandler(IllegalArgumentException.class)
|
||||
public R<?> manualValidationExceptionResponse(IllegalArgumentException exception) {
|
||||
log.error("=========手动校验参数异常=========>>>");
|
||||
log.error(exception.getMessage(), exception);
|
||||
log.error("<<<=========手动校验参数异常=========");
|
||||
return R.fail(ResultStatusEnum.ILLEGAL_ARGUMENT,exception.getMessage());
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,76 +0,0 @@
|
||||
package com.supervision.config;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.supervision.common.domain.R;
|
||||
import com.supervision.common.enums.ResultStatusEnum;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.HttpStatus;
|
||||
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.lang.Nullable;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 统一返回
|
||||
*
|
||||
* @author wb
|
||||
* @date 2022/3/10 13:24
|
||||
*/
|
||||
@Slf4j
|
||||
@RestControllerAdvice(annotations = RestController.class, basePackages = {"com.supervision.**.controller"})
|
||||
public class ResponseConfig implements ResponseBodyAdvice<Object> {
|
||||
|
||||
|
||||
@Override
|
||||
public boolean supports(@Nullable MethodParameter methodParameter,
|
||||
@Nullable Class<? extends HttpMessageConverter<?>> aClass) {
|
||||
assert methodParameter != null;
|
||||
String lowerCase = methodParameter.getDeclaringClass().getName().toLowerCase();
|
||||
log.info(lowerCase);
|
||||
if (lowerCase.contains("swagger") || lowerCase.contains("springdoc")) {
|
||||
return false;
|
||||
} else {
|
||||
log.info("--------{}---------", lowerCase);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("all")
|
||||
public Object beforeBodyWrite(Object o, @Nullable MethodParameter methodParameter, @Nullable MediaType mediaType,
|
||||
@Nullable Class<? extends HttpMessageConverter<?>> aClass, @Nullable ServerHttpRequest serverHttpRequest,
|
||||
@Nullable ServerHttpResponse serverHttpResponse) {
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 添加手动校验参数的异常处理
|
||||
* @param exception 参数验证异常
|
||||
* @return 通用返回值
|
||||
*/
|
||||
@ExceptionHandler(IllegalArgumentException.class)
|
||||
public R<?> manualValidationExceptionResponse(IllegalArgumentException exception) {
|
||||
log.error("=========手动校验参数异常=========>>>");
|
||||
log.error(exception.getMessage(), exception);
|
||||
log.error("<<<=========手动校验参数异常=========");
|
||||
return R.fail(ResultStatusEnum.ILLEGAL_ARGUMENT,exception.getMessage());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue