@@ -6,6 +6,7 @@ import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
import org.springframework.lang.NonNull;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -20,7 +21,7 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
public class RedisTemplateConfig {
|
||||
|
||||
@Bean
|
||||
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
|
||||
public RedisTemplate<String, Object> redisTemplate(@NonNull RedisConnectionFactory connectionFactory) {
|
||||
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
||||
// 设置 RedisTemplate 的连接工厂
|
||||
redisTemplate.setConnectionFactory(connectionFactory);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.jy.xiaoyishu.auth.controller;
|
||||
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
@@ -32,7 +33,8 @@ public class TestController {
|
||||
|
||||
@PostMapping("/test2")
|
||||
@ApiOperationLog(description = "测试接口2")
|
||||
public Response<User> test2(@RequestBody User user) {
|
||||
public Response<User> test2(@RequestBody @Validated User user) {
|
||||
// int i = 1 / 0;
|
||||
return Response.success(user);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.jy.xiaoyishu.auth.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import java.lang.String;
|
||||
|
||||
import com.jy.framework.common.exception.BaseExceptionInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author ayi
|
||||
* @version V1.0
|
||||
* @title ResponseCodeEnum
|
||||
* @description 响应码枚举类
|
||||
* @date 2026/01/15 14:37
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum ResponseCodeEnum implements BaseExceptionInterface {
|
||||
// -------- 通用异常状态码 --------
|
||||
SYSTEM_ERROR("AUTH-10000", "系统错误"), PARAM_NOT_VALID("AUTH-10001", "参数错误");
|
||||
|
||||
/**
|
||||
* 错误码
|
||||
*/
|
||||
private final String errorCode;
|
||||
/**
|
||||
* 错误信息
|
||||
*/
|
||||
private final String errorMessage;
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.jy.xiaoyishu.auth.exception;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import com.jy.framework.common.exception.BizException;
|
||||
import com.jy.framework.common.response.Response;
|
||||
import com.jy.xiaoyishu.auth.enums.ResponseCodeEnum;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@ControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
/**
|
||||
* 捕获自定义业务异常
|
||||
*
|
||||
* @param request 请求
|
||||
* @param e 异常
|
||||
* @return 响应结果
|
||||
*/
|
||||
@ExceptionHandler({BizException.class})
|
||||
@ResponseBody
|
||||
public Response<Object> handleBizException(HttpServletRequest request, BizException e) {
|
||||
log.warn("{} request failure, errorCode: {}, errorMessage: {}", request.getRequestURI(), e.getErrorCode(),
|
||||
e.getErrorMessage());
|
||||
return Response.failure(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* 捕获参数校验异常
|
||||
*
|
||||
* @param request 请求
|
||||
* @param e 异常
|
||||
* @return 响应结果
|
||||
*/
|
||||
@ExceptionHandler({MethodArgumentNotValidException.class})
|
||||
@ResponseBody
|
||||
public Response<Object> handleMethodArgumentNotValidException(HttpServletRequest request,
|
||||
MethodArgumentNotValidException e) {
|
||||
// 参数错误异常码
|
||||
String errorCode = ResponseCodeEnum.PARAM_NOT_VALID.getErrorCode();
|
||||
|
||||
// 获取 BindingResult
|
||||
BindingResult bindingResult = e.getBindingResult();
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
// 获取校验不通过的字段,并组合错误信息,格式为: email 邮箱格式不正确, 当前值: '123124qq.com';
|
||||
Optional.ofNullable(bindingResult.getFieldErrors()).ifPresent(errors -> {
|
||||
errors.forEach(error -> sb.append(error.getField()).append(" ").append(error.getDefaultMessage())
|
||||
.append(", 当前值: '").append(error.getRejectedValue()).append("'; ")
|
||||
|
||||
);
|
||||
});
|
||||
|
||||
// 错误信息
|
||||
String errorMessage = sb.toString();
|
||||
|
||||
log.warn("{} request error, errorCode: {}, errorMessage: {}", request.getRequestURI(), errorCode, errorMessage);
|
||||
|
||||
return Response.failure(errorCode, errorMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 其他类型异常
|
||||
*
|
||||
* @param request 请求
|
||||
* @param e 异常
|
||||
* @return 响应结果
|
||||
*/
|
||||
@ExceptionHandler({Exception.class})
|
||||
@ResponseBody
|
||||
public Response<Object> handleOtherException(HttpServletRequest request, Exception e) {
|
||||
log.error("{} request error, ", request.getRequestURI(), e);
|
||||
return Response.failure(ResponseCodeEnum.SYSTEM_ERROR);
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,9 @@ import lombok.NoArgsConstructor;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author ayi
|
||||
@@ -21,7 +24,9 @@ import java.time.LocalDateTime;
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class User implements Serializable {
|
||||
@NotNull(message = "用户ID不能为空")
|
||||
private Long id;
|
||||
@NotBlank(message = "用户名称不能为空")
|
||||
private String name;
|
||||
private Integer age;
|
||||
private LocalDateTime createTime;
|
||||
|
||||
@@ -55,9 +55,9 @@ spring:
|
||||
max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
|
||||
min-idle: 0 # 连接池中的最小空闲连接
|
||||
max-idle: 10 # 连接池中的最大空闲连接
|
||||
output:
|
||||
ansi:
|
||||
enabled: always
|
||||
# output:
|
||||
# ansi:
|
||||
# enabled: always
|
||||
application:
|
||||
name: xiaoyishu-auth
|
||||
|
||||
|
||||
Reference in New Issue
Block a user