Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2021-2024. lWoHvYe(Hongyan Wang)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lwohvye.beans.advice;
import com.lwohvye.core.annotation.RespResultBody;
import com.lwohvye.core.exception.BadRequestException;
import com.lwohvye.core.utils.ThrowableUtils;
import com.lwohvye.core.utils.result.ResultInfo;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Nullable;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import org.springframework.web.util.WebUtils;
import jakarta.persistence.EntityExistsException;
import jakarta.persistence.EntityNotFoundException;
import jakarta.validation.ConstraintViolationException;
import java.lang.annotation.Annotation;
import java.util.Objects;
/**
* 这也是一种统一数据返回的方式。可视情况整合。与{@link RespResultBody}配合使用
* ResponseBodyAdvice 实现了这个接口的类,处理返回的值在传递给 HttpMessageConverter之前。应用场景为spring项目开发过程中,对controller层返回值进行修改增强处理(比如加密、统一返回格式等)。
* 另外还有RequestBodyAdvice用于在请求之前进行一些操作
*
* @date 2021/11/10 12:42 下午
*/
@Slf4j
// @ConditionalOnClass(name = "org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice") // 这种比较适合目标类在上层,无法直接引用的情况,毕竟类名有些长
@ConditionalOnClass(ResponseBodyAdvice.class) // 这个跟上面那种是等价的,Conditional系列不会报ClassNotFound的Exception。这个配置用于实现当exclude WebMVC使用 WebFlux时,不会init该Bean,也不会报错
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) // 在Spring为Web服务时生效
@ControllerAdvice // 这里用@RestControllerAdvice与@ControllerAdvice好像没区别,本质也是处理链,这只是上面的一环
public class ResponseResultBodyAdvice implements ResponseBodyAdvice