🚧 设置分组校验
This commit is contained in:
parent
45fcbe8d7e
commit
54c2581e08
|
@ -23,6 +23,10 @@
|
|||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- mysql 和 mybatis-plus -->
|
||||
<dependency>
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
package com.mall.common.domain.dto;
|
|
@ -0,0 +1,15 @@
|
|||
package com.mall.common.domain.dto.valid;
|
||||
|
||||
public interface ValidationGroups {
|
||||
interface QUERY {
|
||||
}
|
||||
|
||||
interface Add {
|
||||
}
|
||||
|
||||
interface Update {
|
||||
}
|
||||
|
||||
interface DELETE {
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package com.mall.product.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.mall.common.domain.dto.valid.ValidationGroups;
|
||||
import com.mall.common.domain.vo.result.PageResult;
|
||||
import com.mall.common.domain.vo.result.Result;
|
||||
import com.mall.common.domain.vo.result.ResultCodeEnum;
|
||||
|
@ -13,6 +14,7 @@ import io.swagger.v3.oas.annotations.Parameter;
|
|||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -55,7 +57,7 @@ public class BrandController {
|
|||
|
||||
@Operation(summary = "更新品牌", description = "更新品牌")
|
||||
@PutMapping()
|
||||
public Result<String> updateBrand(@Valid @RequestBody BrandDto dto) {
|
||||
public Result<String> updateBrand(@Validated(ValidationGroups.Update.class) @RequestBody BrandDto dto) {
|
||||
brandService.updateBrand(dto);
|
||||
return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
|
||||
}
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
package com.mall.product.domain.dto;
|
||||
|
||||
import com.mall.common.domain.dto.valid.ValidationGroups;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.hibernate.validator.constraints.URL;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
|
@ -14,12 +18,15 @@ import lombok.NoArgsConstructor;
|
|||
public class BrandDto {
|
||||
|
||||
@Schema(name = "brandId", title = "品牌id")
|
||||
@NotNull(message = "品牌id未传", groups = {ValidationGroups.Update.class, ValidationGroups.DELETE.class})
|
||||
private Long brandId;
|
||||
|
||||
@Schema(name = "name", title = "品牌名")
|
||||
@NotBlank(message = "品牌名不能为空", groups = ValidationGroups.Update.class)
|
||||
private String name;
|
||||
|
||||
@Schema(name = "logo", title = "品牌logo地址")
|
||||
@URL(message = "logo地址不合法")
|
||||
private String logo;
|
||||
|
||||
@Schema(name = "descript", title = "介绍")
|
||||
|
|
|
@ -18,26 +18,6 @@
|
|||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.mall</groupId>
|
||||
<artifactId>mall-common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!-- devtools -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
|
@ -2,12 +2,13 @@ package com.mall.thirdParty;
|
|||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
|
||||
@EnableDiscoveryClient
|
||||
@RefreshScope
|
||||
@SpringBootApplication()
|
||||
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
|
||||
public class MallThirdPartyApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MallThirdPartyApplication.class, args);
|
||||
|
|
Loading…
Reference in New Issue