opt:1.Param中@NotBlank和@NotNull国际化。2.音量设置接口。
This commit is contained in:
@@ -1,23 +1,32 @@
|
||||
package org.nl.exception;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.config.language.LangProcess;
|
||||
import org.nl.util.ThrowableUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.MessageSource;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import java.util.Objects;
|
||||
import jakarta.validation.ConstraintViolationException;
|
||||
|
||||
|
||||
/**
|
||||
* 全局异常处理器
|
||||
* @author liejiu
|
||||
*/
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@Autowired(required = false)
|
||||
private MessageSource messageSource;
|
||||
|
||||
/**
|
||||
* 处理所有不可知的异常
|
||||
*/
|
||||
@@ -28,43 +37,92 @@ public class GlobalExceptionHandler {
|
||||
return buildResponseEntity(ApiError.error(e.getMessage()));
|
||||
}
|
||||
|
||||
/**
|
||||
* token 无效的异常拦截
|
||||
* @param e
|
||||
* @return
|
||||
*/
|
||||
// @ExceptionHandler(value = NotLoginException.class)
|
||||
// public ResponseEntity<ApiError> notLoginException(Exception e) {
|
||||
//// log.error(ThrowableUtil.getStackTrace(e));
|
||||
// return buildResponseEntity(ApiError.error(401,"token 失效"));
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* 处理自定义异常
|
||||
*/
|
||||
@ExceptionHandler(value = BadRequestException.class)
|
||||
public ResponseEntity<ApiError> badRequestException(BadRequestException e) {
|
||||
@ExceptionHandler(value = BadRequestException.class)
|
||||
public ResponseEntity<ApiError> badRequestException(BadRequestException e) {
|
||||
// 打印堆栈信息
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
log.info(e.getMessage());
|
||||
return buildResponseEntity(ApiError.error(e.getStatus(),e.getMessage()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理所有接口数据验证异常
|
||||
* 处理 @RequestBody 参数验证异常
|
||||
*/
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseEntity<ApiError> handleMethodArgumentNotValidException(MethodArgumentNotValidException e){
|
||||
// 打印堆栈信息
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
String[] str = Objects.requireNonNull(e.getBindingResult().getAllErrors().get(0).getCodes())[1].split("\\.");
|
||||
|
||||
// 获取第一个验证错误的消息
|
||||
String message = e.getBindingResult().getAllErrors().get(0).getDefaultMessage();
|
||||
String msg = "不能为空";
|
||||
if(msg.equals(message)){
|
||||
message = str[1] + ":" + message;
|
||||
|
||||
// 如果消息是 {key} 格式,手动解析国际化
|
||||
if (message != null && message.startsWith("{") && message.endsWith("}")) {
|
||||
String key = message.substring(1, message.length() - 1);
|
||||
try {
|
||||
// 使用 LangProcess 解析国际化消息
|
||||
message = LangProcess.msg(key);
|
||||
} catch (Exception ex) {
|
||||
log.warn("Failed to resolve i18n message for key: {}", key);
|
||||
// 如果解析失败,尝试使用 MessageSource
|
||||
if (messageSource != null) {
|
||||
try {
|
||||
message = messageSource.getMessage(key, null, LocaleContextHolder.getLocale());
|
||||
} catch (Exception ex2) {
|
||||
log.warn("Failed to resolve message from MessageSource for key: {}", key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.info("Validation error: {}", message);
|
||||
return buildResponseEntity(ApiError.error(message));
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理 @Validated 参数验证异常(用于表单提交)
|
||||
*/
|
||||
@ExceptionHandler(BindException.class)
|
||||
public ResponseEntity<ApiError> handleBindException(BindException e){
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
String message = e.getBindingResult().getAllErrors().get(0).getDefaultMessage();
|
||||
|
||||
// 如果消息是 {key} 格式,手动解析国际化
|
||||
if (message != null && message.startsWith("{") && message.endsWith("}")) {
|
||||
String key = message.substring(1, message.length() - 1);
|
||||
try {
|
||||
message = LangProcess.msg(key);
|
||||
} catch (Exception ex) {
|
||||
log.warn("Failed to resolve i18n message for key: {}", key);
|
||||
}
|
||||
}
|
||||
|
||||
log.info("Bind validation error: {}", message);
|
||||
return buildResponseEntity(ApiError.error(message));
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理 @Validated 参数验证异常(用于方法参数)
|
||||
*/
|
||||
@ExceptionHandler(ConstraintViolationException.class)
|
||||
public ResponseEntity<ApiError> handleConstraintViolationException(ConstraintViolationException e){
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
String message = e.getConstraintViolations().iterator().next().getMessage();
|
||||
|
||||
// 如果消息是 {key} 格式,手动解析国际化
|
||||
if (message != null && message.startsWith("{") && message.endsWith("}")) {
|
||||
String key = message.substring(1, message.length() - 1);
|
||||
try {
|
||||
message = LangProcess.msg(key);
|
||||
} catch (Exception ex) {
|
||||
log.warn("Failed to resolve i18n message for key: {}", key);
|
||||
}
|
||||
}
|
||||
|
||||
log.info("Constraint violation error: {}", message);
|
||||
return buildResponseEntity(ApiError.error(message));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user