Skip to content

Commit

Permalink
使用全局异常处理检验逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
macrozheng committed Sep 5, 2020
1 parent c7b10b5 commit cb95a01
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 83 deletions.
4 changes: 0 additions & 4 deletions mall-admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -36,7 +35,7 @@ public CommonResult<List<PmsBrand>> 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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,7 +42,7 @@ public CommonResult<CommonPage<PmsProductAttribute>> 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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -50,7 +49,7 @@ public CommonResult<PmsProductResult> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
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.*;

import java.util.HashMap;
Expand All @@ -40,7 +40,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();
Expand All @@ -51,7 +51,7 @@ public CommonResult<UmsAdmin> 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) {
return adminService.login(umsAdminLoginParam.getUsername(),umsAdminLoginParam.getPassword());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
@EqualsAndHashCode(callSuper = false)
public class UmsAdminLoginParam {
@ApiModelProperty(value = "用户名", required = true)
@NotEmpty(message = "用户名不能为空")
@NotEmpty
private String username;
@ApiModelProperty(value = "密码", required = true)
@NotEmpty(message = "密码不能为空")
@NotEmpty
private String password;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
@Setter
public class UmsAdminParam {
@ApiModelProperty(value = "用户名", required = true)
@NotEmpty(message = "用户名不能为空")
@NotEmpty
private String username;
@ApiModelProperty(value = "密码", required = true)
@NotEmpty(message = "密码不能为空")
@NotEmpty
private String password;
@ApiModelProperty(value = "用户头像")
private String icon;
@ApiModelProperty(value = "邮箱")
@Email(message = "邮箱格式不合法")
@Email
private String email;
@ApiModelProperty(value = "用户昵称")
private String nickName;
Expand Down
4 changes: 4 additions & 0 deletions mall-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}
4 changes: 0 additions & 4 deletions mall-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -38,10 +37,7 @@ public CommonResult<List<PmsBrand>> 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) {
Expand All @@ -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) {
Expand Down

0 comments on commit cb95a01

Please sign in to comment.