使用全局异常处理检验逻辑
This commit is contained in:
parent
70fd6b20d3
commit
b57f7e7e97
@ -33,10 +33,6 @@
|
||||
<groupId>io.minio</groupId>
|
||||
<artifactId>minio</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.validation</groupId>
|
||||
<artifactId>jakarta.validation-api</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
@ -9,7 +9,6 @@ import io.swagger.annotations.Api;
|
||||
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 class PmsBrandController {
|
||||
@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 class PmsBrandController {
|
||||
@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) {
|
||||
|
@ -12,7 +12,6 @@ import io.swagger.annotations.ApiImplicitParams;
|
||||
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 class PmsProductAttributeController {
|
||||
@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 class PmsProductAttributeController {
|
||||
@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);
|
||||
|
@ -10,7 +10,6 @@ import io.swagger.annotations.Api;
|
||||
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 class PmsProductCategoryController {
|
||||
@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);
|
||||
|
@ -11,7 +11,6 @@ import io.swagger.annotations.Api;
|
||||
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 class PmsProductController {
|
||||
@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);
|
||||
|
@ -16,7 +16,7 @@ import io.swagger.annotations.ApiOperation;
|
||||
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<UmsAdmin> register(@RequestBody UmsAdminParam umsAdminParam, BindingResult result) {
|
||||
public CommonResult<UmsAdmin> register(@Validated @RequestBody UmsAdminParam umsAdminParam) {
|
||||
UmsAdmin umsAdmin = adminService.register(umsAdminParam);
|
||||
if (umsAdmin == null) {
|
||||
return CommonResult.failed();
|
||||
@ -57,7 +57,7 @@ public class UmsAdminController {
|
||||
@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 class UmsAdminController {
|
||||
@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);
|
||||
|
@ -47,6 +47,10 @@
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -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 class GlobalExceptionHandler {
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -33,10 +33,6 @@
|
||||
<groupId>net.logstash.logback</groupId>
|
||||
<artifactId>logstash-logback-encoder</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.validation</groupId>
|
||||
<artifactId>jakarta.validation-api</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -11,7 +11,6 @@ import org.slf4j.Logger;
|
||||
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 class DemoController {
|
||||
@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 class DemoController {
|
||||
@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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user