Compare commits
3 Commits
585dd4290d
...
86104b6bdf
Author | SHA1 | Date |
---|---|---|
|
86104b6bdf | |
|
54c2581e08 | |
|
45fcbe8d7e |
|
@ -23,38 +23,11 @@
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.github.ben-manes.caffeine</groupId>
|
|
||||||
<artifactId>caffeine</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
<artifactId>spring-boot-starter-validation</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- alibaba -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.alibaba.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.alibaba.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<!-- <dependency> -->
|
|
||||||
<!-- <groupId>com.alibaba.cloud</groupId> -->
|
|
||||||
<!-- <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> -->
|
|
||||||
<!-- </dependency> -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<!-- <dependency> -->
|
|
||||||
<!-- <groupId>org.springframework.cloud</groupId> -->
|
|
||||||
<!-- <artifactId>spring-cloud-starter-openfeign</artifactId> -->
|
|
||||||
<!-- </dependency> -->
|
|
||||||
|
|
||||||
<!-- mysql 和 mybatis-plus -->
|
<!-- mysql 和 mybatis-plus -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.baomidou</groupId>
|
<groupId>com.baomidou</groupId>
|
||||||
|
@ -75,18 +48,5 @@
|
||||||
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
|
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- 前端美化相关 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.webjars</groupId>
|
|
||||||
<artifactId>bootstrap</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.webjars</groupId>
|
|
||||||
<artifactId>font-awesome</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.webjars</groupId>
|
|
||||||
<artifactId>jquery</artifactId>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
package com.mall.common.domain.dto;
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.mall.common.domain.dto.valid.custom;
|
||||||
|
|
||||||
|
import jakarta.validation.Constraint;
|
||||||
|
import jakarta.validation.Payload;
|
||||||
|
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import static java.lang.annotation.ElementType.*;
|
||||||
|
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||||
|
|
||||||
|
@Documented
|
||||||
|
@Constraint(validatedBy = {ListValueConstraintValidator.class})
|
||||||
|
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
|
||||||
|
@Retention(RUNTIME)
|
||||||
|
public @interface ListValue {
|
||||||
|
|
||||||
|
String message() default "{com.mall.common.domain.dto.valid.custom.ListValue.message}";
|
||||||
|
|
||||||
|
Class<?>[] groups() default {};
|
||||||
|
|
||||||
|
Class<? extends Payload>[] payload() default {};
|
||||||
|
|
||||||
|
int[] vals() default {};
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
package com.mall.common.domain.dto.valid.custom;
|
||||||
|
|
||||||
|
import jakarta.validation.ConstraintValidator;
|
||||||
|
import jakarta.validation.ConstraintValidatorContext;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {
|
||||||
|
private final Set<Integer> set = new HashSet<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the validator in preparation for
|
||||||
|
* calls.
|
||||||
|
* The constraint annotation for a given constraint declaration
|
||||||
|
* is passed.
|
||||||
|
* <p>
|
||||||
|
* This method is guaranteed to be called before any use of this instance for
|
||||||
|
* validation.
|
||||||
|
* <p>
|
||||||
|
* The default implementation is a no-op.
|
||||||
|
*
|
||||||
|
* @param constraintAnnotation annotation instance for a given constraint declaration
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void initialize(ListValue constraintAnnotation) {
|
||||||
|
int[] vals = constraintAnnotation.vals();
|
||||||
|
for (int val : vals) {
|
||||||
|
set.add(val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements the validation logic.
|
||||||
|
* The state of {@code value} must not be altered.
|
||||||
|
* <p>
|
||||||
|
* This method can be accessed concurrently, thread-safety must be ensured
|
||||||
|
* by the implementation.
|
||||||
|
*
|
||||||
|
* @param value object to validate
|
||||||
|
* @param context context in which the constraint is evaluated
|
||||||
|
* @return {@code false} if {@code value} does not pass the constraint
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean isValid(Integer value, ConstraintValidatorContext context) {
|
||||||
|
return set.contains(value);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package com.mall.common.domain.dto.valid.group;
|
||||||
|
|
||||||
|
public interface AddGroup {
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package com.mall.common.domain.dto.valid.group;
|
||||||
|
|
||||||
|
public interface DeletedGroup {
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package com.mall.common.domain.dto.valid.group;
|
||||||
|
|
||||||
|
public interface QueryGroup {
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
package com.mall.common.domain.dto.valid.group;
|
||||||
|
|
||||||
|
public interface UpdateGroup {
|
||||||
|
}
|
|
@ -0,0 +1,95 @@
|
||||||
|
package com.mall.common.exception;
|
||||||
|
|
||||||
|
import com.mall.common.domain.vo.result.Result;
|
||||||
|
import com.mall.common.domain.vo.result.ResultCodeEnum;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
import java.sql.SQLIntegrityConstraintViolationException;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@RestControllerAdvice
|
||||||
|
@Slf4j
|
||||||
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
|
@ExceptionHandler(MallException.class)
|
||||||
|
@ResponseBody
|
||||||
|
public Result<Object> exceptionHandler(MallException exception) {
|
||||||
|
String message = exception.getMessage();
|
||||||
|
Integer code = exception.getCode();
|
||||||
|
return Result.error(null, code, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(RuntimeException.class)
|
||||||
|
@ResponseBody
|
||||||
|
public Result<Object> exceptionHandler(RuntimeException exception) {
|
||||||
|
String message = exception.getMessage();
|
||||||
|
message = StringUtils.hasText(message) ? message : "服务器异常";
|
||||||
|
exception.printStackTrace();
|
||||||
|
|
||||||
|
// 解析异常
|
||||||
|
String jsonParseError = "JSON parse error (.*)";
|
||||||
|
Matcher jsonParseErrorMatcher = Pattern.compile(jsonParseError).matcher(message);
|
||||||
|
if (jsonParseErrorMatcher.find()) {
|
||||||
|
return Result.error(null, 500, "JSON解析异常 " + jsonParseErrorMatcher.group(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 数据过大
|
||||||
|
String dataTooLongError = "Data too long for column (.*?) at row 1";
|
||||||
|
Matcher dataTooLongErrorMatcher = Pattern.compile(dataTooLongError).matcher(message);
|
||||||
|
if (dataTooLongErrorMatcher.find()) {
|
||||||
|
return Result.error(null, 500, dataTooLongErrorMatcher.group(1) + " 字段数据过大");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 主键冲突
|
||||||
|
String primaryKeyError = "Duplicate entry '(.*?)' for key .*";
|
||||||
|
Matcher primaryKeyErrorMatcher = Pattern.compile(primaryKeyError).matcher(message);
|
||||||
|
if (primaryKeyErrorMatcher.find()) {
|
||||||
|
return Result.error(null, 500, "[" + primaryKeyErrorMatcher.group(1) + "]已存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
log.error("GlobalExceptionHandler===>运行时异常信息:{}", message);
|
||||||
|
return Result.error(null, 500, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 表单验证字段
|
||||||
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
|
public Result<String> handleValidationExceptions(MethodArgumentNotValidException ex) {
|
||||||
|
String errorMessage = ex.getBindingResult().getFieldErrors().stream()
|
||||||
|
.map(DefaultMessageSourceResolvable::getDefaultMessage)
|
||||||
|
.distinct()
|
||||||
|
.collect(Collectors.joining(", "));
|
||||||
|
return Result.error(null, 201, errorMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 特定异常处理
|
||||||
|
@ExceptionHandler(ArithmeticException.class)
|
||||||
|
@ResponseBody
|
||||||
|
public Result<Object> error(ArithmeticException exception) {
|
||||||
|
log.error("GlobalExceptionHandler===>特定异常信息:{}", exception.getMessage());
|
||||||
|
|
||||||
|
return Result.error(null, 500, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理SQL异常
|
||||||
|
@ExceptionHandler(SQLIntegrityConstraintViolationException.class)
|
||||||
|
@ResponseBody
|
||||||
|
public Result<String> exceptionHandler(SQLIntegrityConstraintViolationException exception) {
|
||||||
|
log.error("GlobalExceptionHandler===>处理SQL异常:{}", exception.getMessage());
|
||||||
|
|
||||||
|
String message = exception.getMessage();
|
||||||
|
if (message.contains("Duplicate entry")) {
|
||||||
|
// 错误信息
|
||||||
|
return Result.error(ResultCodeEnum.USER_IS_EMPTY);
|
||||||
|
} else {
|
||||||
|
return Result.error(ResultCodeEnum.UNKNOWN_EXCEPTION);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
package com.mall.common.exception;
|
||||||
|
|
||||||
|
import com.mall.common.domain.vo.result.ResultCodeEnum;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.ToString;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Getter
|
||||||
|
@ToString
|
||||||
|
@Slf4j
|
||||||
|
public class MallException extends RuntimeException {
|
||||||
|
private Integer code;
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
public MallException(Integer code, String message) {
|
||||||
|
super(message);
|
||||||
|
this.code = code;
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MallException(String message) {
|
||||||
|
super(message);
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MallException(ResultCodeEnum codeEnum) {
|
||||||
|
super(codeEnum.getMessage());
|
||||||
|
this.code = codeEnum.getCode();
|
||||||
|
this.message = codeEnum.getMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
public MallException(String message, Exception exception) {
|
||||||
|
super(message);
|
||||||
|
this.message = message;
|
||||||
|
log.error(message, exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
com.mall.common.domain.dto.valid.custom.ListValue.message=\u503C\u4E0D\u5728\u6307\u5B9A\u8303\u56F4\u4E2D
|
|
@ -1,58 +0,0 @@
|
||||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
<parent>
|
|
||||||
<groupId>com.mall</groupId>
|
|
||||||
<artifactId>mall-cloud</artifactId>
|
|
||||||
<version>1.0-SNAPSHOT</version>
|
|
||||||
</parent>
|
|
||||||
|
|
||||||
<artifactId>mall-coupon</artifactId>
|
|
||||||
<packaging>jar</packaging>
|
|
||||||
|
|
||||||
<name>mall-coupon</name>
|
|
||||||
<url>https://maven.apache.org</url>
|
|
||||||
|
|
||||||
<properties>
|
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.mall</groupId>
|
|
||||||
<artifactId>mall-common</artifactId>
|
|
||||||
<version>1.0-SNAPSHOT</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- devtools -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-devtools</artifactId>
|
|
||||||
<scope>runtime</scope>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- alibaba -->
|
|
||||||
<!-- <dependency> -->
|
|
||||||
<!-- <groupId>com.alibaba.cloud</groupId> -->
|
|
||||||
<!-- <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> -->
|
|
||||||
<!-- </dependency> -->
|
|
||||||
<!-- <dependency> -->
|
|
||||||
<!-- <groupId>org.springframework.cloud</groupId> -->
|
|
||||||
<!-- <artifactId>spring-cloud-starter-loadbalancer</artifactId> -->
|
|
||||||
<!-- </dependency> -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
</project>
|
|
|
@ -1,58 +0,0 @@
|
||||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
<parent>
|
|
||||||
<groupId>com.mall</groupId>
|
|
||||||
<artifactId>mall-cloud</artifactId>
|
|
||||||
<version>1.0-SNAPSHOT</version>
|
|
||||||
</parent>
|
|
||||||
|
|
||||||
<artifactId>mall-member</artifactId>
|
|
||||||
<packaging>jar</packaging>
|
|
||||||
|
|
||||||
<name>mall-member</name>
|
|
||||||
<url>https://maven.apache.org</url>
|
|
||||||
|
|
||||||
<properties>
|
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.mall</groupId>
|
|
||||||
<artifactId>mall-common</artifactId>
|
|
||||||
<version>1.0-SNAPSHOT</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- devtools -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-devtools</artifactId>
|
|
||||||
<scope>runtime</scope>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- alibaba -->
|
|
||||||
<!-- <dependency> -->
|
|
||||||
<!-- <groupId>com.alibaba.cloud</groupId> -->
|
|
||||||
<!-- <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> -->
|
|
||||||
<!-- </dependency> -->
|
|
||||||
<!-- <dependency> -->
|
|
||||||
<!-- <groupId>org.springframework.cloud</groupId> -->
|
|
||||||
<!-- <artifactId>spring-cloud-starter-loadbalancer</artifactId> -->
|
|
||||||
<!-- </dependency> -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
</project>
|
|
Before Width: | Height: | Size: 66 KiB After Width: | Height: | Size: 66 KiB |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue