diff --git a/mall-admin/pom.xml b/mall-admin/pom.xml
index 6a4d037233..af68190f7b 100644
--- a/mall-admin/pom.xml
+++ b/mall-admin/pom.xml
@@ -33,10 +33,6 @@
io.minio
minio
-
- jakarta.validation
- jakarta.validation-api
-
diff --git a/mall-admin/src/main/java/com/macro/mall/component/BindingResultAspect.java b/mall-admin/src/main/java/com/macro/mall/component/BindingResultAspect.java
deleted file mode 100644
index f7fb2b32db..0000000000
--- a/mall-admin/src/main/java/com/macro/mall/component/BindingResultAspect.java
+++ /dev/null
@@ -1,43 +0,0 @@
-package com.macro.mall.component;
-
-import com.macro.mall.common.api.CommonResult;
-import org.aspectj.lang.ProceedingJoinPoint;
-import org.aspectj.lang.annotation.Around;
-import org.aspectj.lang.annotation.Aspect;
-import org.aspectj.lang.annotation.Pointcut;
-import org.springframework.core.annotation.Order;
-import org.springframework.stereotype.Component;
-import org.springframework.validation.BindingResult;
-import org.springframework.validation.FieldError;
-
-/**
- * HibernateValidator错误结果处理切面
- * Created by macro on 2018/4/26.
- */
-@Aspect
-@Component
-@Order(2)
-public class BindingResultAspect {
- @Pointcut("execution(public * com.macro.mall.controller.*.*(..))")
- public void BindingResult() {
- }
-
- @Around("BindingResult()")
- public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
- Object[] args = joinPoint.getArgs();
- for (Object arg : args) {
- if (arg instanceof BindingResult) {
- BindingResult result = (BindingResult) arg;
- if (result.hasErrors()) {
- FieldError fieldError = result.getFieldError();
- if(fieldError!=null){
- return CommonResult.validateFailed(fieldError.getDefaultMessage());
- }else{
- return CommonResult.validateFailed();
- }
- }
- }
- }
- return joinPoint.proceed();
- }
-}
diff --git a/mall-admin/src/main/java/com/macro/mall/controller/PmsBrandController.java b/mall-admin/src/main/java/com/macro/mall/controller/PmsBrandController.java
index e793904bc7..b5d89dee51 100644
--- a/mall-admin/src/main/java/com/macro/mall/controller/PmsBrandController.java
+++ b/mall-admin/src/main/java/com/macro/mall/controller/PmsBrandController.java
@@ -9,7 +9,6 @@
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
-import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@@ -36,7 +35,7 @@ public CommonResult> getList() {
@ApiOperation(value = "添加品牌")
@RequestMapping(value = "/create", method = RequestMethod.POST)
@ResponseBody
- public CommonResult create(@Validated @RequestBody PmsBrandParam pmsBrand, BindingResult result) {
+ public CommonResult create(@Validated @RequestBody PmsBrandParam pmsBrand) {
CommonResult commonResult;
int count = brandService.createBrand(pmsBrand);
if (count == 1) {
@@ -51,8 +50,7 @@ public CommonResult create(@Validated @RequestBody PmsBrandParam pmsBrand, Bindi
@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
@ResponseBody
public CommonResult update(@PathVariable("id") Long id,
- @Validated @RequestBody PmsBrandParam pmsBrandParam,
- BindingResult result) {
+ @Validated @RequestBody PmsBrandParam pmsBrandParam) {
CommonResult commonResult;
int count = brandService.updateBrand(id, pmsBrandParam);
if (count == 1) {
diff --git a/mall-admin/src/main/java/com/macro/mall/controller/PmsProductAttributeController.java b/mall-admin/src/main/java/com/macro/mall/controller/PmsProductAttributeController.java
index e375491e79..ed82bcf9ca 100644
--- a/mall-admin/src/main/java/com/macro/mall/controller/PmsProductAttributeController.java
+++ b/mall-admin/src/main/java/com/macro/mall/controller/PmsProductAttributeController.java
@@ -12,7 +12,6 @@
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
-import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@@ -43,7 +42,7 @@ public CommonResult> getList(@PathVariable Long
@ApiOperation("添加商品属性信息")
@RequestMapping(value = "/create", method = RequestMethod.POST)
@ResponseBody
- public CommonResult create(@RequestBody PmsProductAttributeParam productAttributeParam, BindingResult bindingResult) {
+ public CommonResult create(@RequestBody PmsProductAttributeParam productAttributeParam) {
int count = productAttributeService.create(productAttributeParam);
if (count > 0) {
return CommonResult.success(count);
@@ -55,7 +54,7 @@ public CommonResult create(@RequestBody PmsProductAttributeParam productAttribut
@ApiOperation("修改商品属性信息")
@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
@ResponseBody
- public CommonResult update(@PathVariable Long id, @RequestBody PmsProductAttributeParam productAttributeParam, BindingResult bindingResult) {
+ public CommonResult update(@PathVariable Long id, @RequestBody PmsProductAttributeParam productAttributeParam) {
int count = productAttributeService.update(id, productAttributeParam);
if (count > 0) {
return CommonResult.success(count);
diff --git a/mall-admin/src/main/java/com/macro/mall/controller/PmsProductCategoryController.java b/mall-admin/src/main/java/com/macro/mall/controller/PmsProductCategoryController.java
index 71f48cd970..50a616a8aa 100644
--- a/mall-admin/src/main/java/com/macro/mall/controller/PmsProductCategoryController.java
+++ b/mall-admin/src/main/java/com/macro/mall/controller/PmsProductCategoryController.java
@@ -10,7 +10,6 @@
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
-import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@@ -30,8 +29,7 @@ public class PmsProductCategoryController {
@ApiOperation("添加产品分类")
@RequestMapping(value = "/create", method = RequestMethod.POST)
@ResponseBody
- public CommonResult create(@Validated @RequestBody PmsProductCategoryParam productCategoryParam,
- BindingResult result) {
+ public CommonResult create(@Validated @RequestBody PmsProductCategoryParam productCategoryParam) {
int count = productCategoryService.create(productCategoryParam);
if (count > 0) {
return CommonResult.success(count);
@@ -45,8 +43,7 @@ public CommonResult create(@Validated @RequestBody PmsProductCategoryParam produ
@ResponseBody
public CommonResult update(@PathVariable Long id,
@Validated
- @RequestBody PmsProductCategoryParam productCategoryParam,
- BindingResult result) {
+ @RequestBody PmsProductCategoryParam productCategoryParam) {
int count = productCategoryService.update(id, productCategoryParam);
if (count > 0) {
return CommonResult.success(count);
diff --git a/mall-admin/src/main/java/com/macro/mall/controller/PmsProductController.java b/mall-admin/src/main/java/com/macro/mall/controller/PmsProductController.java
index 8ac63aa68d..27afe82c1b 100644
--- a/mall-admin/src/main/java/com/macro/mall/controller/PmsProductController.java
+++ b/mall-admin/src/main/java/com/macro/mall/controller/PmsProductController.java
@@ -11,7 +11,6 @@
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
-import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@@ -30,7 +29,7 @@ public class PmsProductController {
@ApiOperation("创建商品")
@RequestMapping(value = "/create", method = RequestMethod.POST)
@ResponseBody
- public CommonResult create(@RequestBody PmsProductParam productParam, BindingResult bindingResult) {
+ public CommonResult create(@RequestBody PmsProductParam productParam) {
int count = productService.create(productParam);
if (count > 0) {
return CommonResult.success(count);
@@ -50,7 +49,7 @@ public CommonResult getUpdateInfo(@PathVariable Long id) {
@ApiOperation("更新商品")
@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
@ResponseBody
- public CommonResult update(@PathVariable Long id, @RequestBody PmsProductParam productParam, BindingResult bindingResult) {
+ public CommonResult update(@PathVariable Long id, @RequestBody PmsProductParam productParam) {
int count = productService.update(id, productParam);
if (count > 0) {
return CommonResult.success(count);
diff --git a/mall-admin/src/main/java/com/macro/mall/controller/UmsAdminController.java b/mall-admin/src/main/java/com/macro/mall/controller/UmsAdminController.java
index 1225d03d63..02cd161151 100644
--- a/mall-admin/src/main/java/com/macro/mall/controller/UmsAdminController.java
+++ b/mall-admin/src/main/java/com/macro/mall/controller/UmsAdminController.java
@@ -16,7 +16,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
-import org.springframework.validation.BindingResult;
+import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@@ -46,7 +46,7 @@ public class UmsAdminController {
@ApiOperation(value = "用户注册")
@RequestMapping(value = "/register", method = RequestMethod.POST)
@ResponseBody
- public CommonResult register(@RequestBody UmsAdminParam umsAdminParam, BindingResult result) {
+ public CommonResult register(@Validated @RequestBody UmsAdminParam umsAdminParam) {
UmsAdmin umsAdmin = adminService.register(umsAdminParam);
if (umsAdmin == null) {
return CommonResult.failed();
@@ -57,7 +57,7 @@ public CommonResult register(@RequestBody UmsAdminParam umsAdminParam,
@ApiOperation(value = "登录以后返回token")
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
- public CommonResult login(@RequestBody UmsAdminLoginParam umsAdminLoginParam, BindingResult result) {
+ public CommonResult login(@Validated @RequestBody UmsAdminLoginParam umsAdminLoginParam) {
String token = adminService.login(umsAdminLoginParam.getUsername(), umsAdminLoginParam.getPassword());
if (token == null) {
return CommonResult.validateFailed("用户名或密码错误");
@@ -143,7 +143,7 @@ public CommonResult update(@PathVariable Long id, @RequestBody UmsAdmin admin) {
@ApiOperation("修改指定用户密码")
@RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
@ResponseBody
- public CommonResult updatePassword(@RequestBody UpdateAdminPasswordParam updatePasswordParam) {
+ public CommonResult updatePassword(@Validated @RequestBody UpdateAdminPasswordParam updatePasswordParam) {
int status = adminService.updatePassword(updatePasswordParam);
if (status > 0) {
return CommonResult.success(status);
diff --git a/mall-common/pom.xml b/mall-common/pom.xml
index 9adf16d423..7d96abbc03 100644
--- a/mall-common/pom.xml
+++ b/mall-common/pom.xml
@@ -47,6 +47,10 @@
io.springfox
springfox-swagger-ui
+
+ org.springframework.boot
+ spring-boot-starter-validation
+
\ No newline at end of file
diff --git a/mall-common/src/main/java/com/macro/mall/common/exception/GlobalExceptionHandler.java b/mall-common/src/main/java/com/macro/mall/common/exception/GlobalExceptionHandler.java
index 1456f4652a..e95fd8a58b 100644
--- a/mall-common/src/main/java/com/macro/mall/common/exception/GlobalExceptionHandler.java
+++ b/mall-common/src/main/java/com/macro/mall/common/exception/GlobalExceptionHandler.java
@@ -1,6 +1,10 @@
package com.macro.mall.common.exception;
import com.macro.mall.common.api.CommonResult;
+import org.springframework.validation.BindException;
+import org.springframework.validation.BindingResult;
+import org.springframework.validation.FieldError;
+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;
@@ -20,4 +24,32 @@ public CommonResult handle(ApiException e) {
}
return CommonResult.failed(e.getMessage());
}
+
+ @ResponseBody
+ @ExceptionHandler(value = MethodArgumentNotValidException.class)
+ public CommonResult handleValidException(MethodArgumentNotValidException e) {
+ BindingResult bindingResult = e.getBindingResult();
+ String message = null;
+ if (bindingResult.hasErrors()) {
+ FieldError fieldError = bindingResult.getFieldError();
+ if (fieldError != null) {
+ message = fieldError.getField()+fieldError.getDefaultMessage();
+ }
+ }
+ return CommonResult.validateFailed(message);
+ }
+
+ @ResponseBody
+ @ExceptionHandler(value = BindException.class)
+ public CommonResult handleValidException(BindException e) {
+ BindingResult bindingResult = e.getBindingResult();
+ String message = null;
+ if (bindingResult.hasErrors()) {
+ FieldError fieldError = bindingResult.getFieldError();
+ if (fieldError != null) {
+ message = fieldError.getField()+fieldError.getDefaultMessage();
+ }
+ }
+ return CommonResult.validateFailed(message);
+ }
}
diff --git a/mall-demo/pom.xml b/mall-demo/pom.xml
index 61247f082c..2fc739b949 100644
--- a/mall-demo/pom.xml
+++ b/mall-demo/pom.xml
@@ -33,10 +33,6 @@
net.logstash.logback
logstash-logback-encoder
-
- jakarta.validation
- jakarta.validation-api
-
diff --git a/mall-demo/src/main/java/com/macro/mall/demo/controller/DemoController.java b/mall-demo/src/main/java/com/macro/mall/demo/controller/DemoController.java
index c40a7ede60..ae72ae79a4 100644
--- a/mall-demo/src/main/java/com/macro/mall/demo/controller/DemoController.java
+++ b/mall-demo/src/main/java/com/macro/mall/demo/controller/DemoController.java
@@ -11,7 +11,6 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
-import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@@ -38,10 +37,7 @@ public CommonResult> getBrandList() {
@ApiOperation(value = "添加品牌")
@RequestMapping(value = "/brand/create", method = RequestMethod.POST)
@ResponseBody
- public CommonResult createBrand(@Validated @RequestBody PmsBrandDto pmsBrand, BindingResult result) {
- if (result.hasErrors()) {
- return CommonResult.validateFailed(result.getFieldError().getDefaultMessage());
- }
+ public CommonResult createBrand(@Validated @RequestBody PmsBrandDto pmsBrand) {
CommonResult commonResult;
int count = demoService.createBrand(pmsBrand);
if (count == 1) {
@@ -57,10 +53,7 @@ public CommonResult createBrand(@Validated @RequestBody PmsBrandDto pmsBrand, Bi
@ApiOperation(value = "更新品牌")
@RequestMapping(value = "/brand/update/{id}", method = RequestMethod.POST)
@ResponseBody
- public CommonResult updateBrand(@PathVariable("id") Long id, @Validated @RequestBody PmsBrandDto pmsBrandDto,BindingResult result) {
- if(result.hasErrors()){
- return CommonResult.validateFailed(result.getFieldError().getDefaultMessage());
- }
+ public CommonResult updateBrand(@PathVariable("id") Long id, @Validated @RequestBody PmsBrandDto pmsBrandDto) {
CommonResult commonResult;
int count = demoService.updateBrand(id, pmsBrandDto);
if (count == 1) {