1.validation数据校验优化

This commit is contained in:
mazhicheng 2019-10-15 17:31:15 +08:00
parent 3c6819434b
commit 1ce30923ae
2 changed files with 12 additions and 20 deletions

View File

@ -87,7 +87,7 @@ public abstract class BaseCrudRestController extends BaseController {
protected JsonResult createEntity(BaseEntity entity, BindingResult result) throws Exception {
// Model属性值验证结果
if (result != null && result.hasErrors()) {
return new JsonResult(Status.FAIL_INVALID_PARAM, super.getBindingError(result));
return new JsonResult(Status.FAIL_VALIDATION, super.getBindingError(result));
}
// 执行创建资源前的操作
String validateResult = this.beforeCreate(entity);
@ -104,7 +104,7 @@ public abstract class BaseCrudRestController extends BaseController {
data.put(PARAM_ID, entity.getId());
return new JsonResult(Status.OK, data);
} else {
log.warn("创建操作未成功,model=" + entity.getClass().getSimpleName());
log.warn("创建操作未成功,entity=" + entity.getClass().getSimpleName());
// 组装返回结果
return new JsonResult(Status.FAIL_OPERATION);
}
@ -118,9 +118,9 @@ public abstract class BaseCrudRestController extends BaseController {
* @throws Exception
*/
protected JsonResult updateEntity(BaseEntity entity, BindingResult result) throws Exception {
// Model属性值验证结果
// Entity属性值验证结果
if (result.hasErrors()) {
return new JsonResult(Status.FAIL_INVALID_PARAM, super.getBindingError(result));
return new JsonResult(Status.FAIL_VALIDATION, super.getBindingError(result));
}
// 执行更新资源前的操作
String validateResult = this.beforeUpdate(entity);
@ -137,7 +137,7 @@ public abstract class BaseCrudRestController extends BaseController {
data.put(PARAM_ID, entity.getId());
return new JsonResult(Status.OK, data);
} else {
log.warn("更新操作失败,model=" + entity.getClass().getSimpleName() + ", id=" + entity.getId());
log.warn("更新操作失败,{}:{}", entity.getClass().getSimpleName(), entity.getId());
// 返回操作结果
return new JsonResult(Status.FAIL_OPERATION);
}
@ -154,9 +154,9 @@ public abstract class BaseCrudRestController extends BaseController {
return new JsonResult(Status.FAIL_INVALID_PARAM, "请选择需要删除的条目!");
}
// 是否有权限删除
BaseEntity model = (BaseEntity) getService().getEntity(id);
BaseEntity entity = (BaseEntity) getService().getEntity(id);
// 执行删除操作
String validateResult = beforeDelete(model);
String validateResult = beforeDelete(entity);
if (validateResult != null) {
// 返回json
return new JsonResult(Status.FAIL_OPERATION, validateResult);
@ -164,13 +164,13 @@ public abstract class BaseCrudRestController extends BaseController {
// 执行删除操作
boolean success = getService().deleteEntity(id);
if (success) {
log.info("删除操作成功,model=" + model.getClass().getSimpleName() + ", id=" + id);
log.info("删除操作成功,{}:{}", entity.getClass().getSimpleName(), id);
// 组装返回结果
Map<String, Object> data = new HashMap<>(2);
data.put(PARAM_ID, model.getId());
data.put(PARAM_ID, entity.getId());
return new JsonResult(Status.OK, data);
} else {
log.warn("删除操作未成功,model=" + model.getClass().getSimpleName() + ", id=" + id);
log.warn("删除操作未成功,{}:{}", entity.getClass().getSimpleName(), id);
return new JsonResult(Status.FAIL_OPERATION);
}
}

View File

@ -104,11 +104,7 @@ public class DepartmentController extends BaseCrudRestController {
@AuthorizationWrapper(value = @RequiresPermissions("create"), name = "新建")
public JsonResult createEntity(@RequestBody Department entity, BindingResult result, HttpServletRequest request)
throws Exception{
boolean success = departmentService.createEntity(entity);
if(!success){
return new JsonResult(Status.FAIL_OPERATION);
}
return new JsonResult(Status.OK);
return super.createEntity(entity, result);
}
/***
@ -136,11 +132,7 @@ public class DepartmentController extends BaseCrudRestController {
public JsonResult updateModel(@PathVariable("id")Long id, @RequestBody Department entity, BindingResult result,
HttpServletRequest request) throws Exception{
entity.setId(id);
boolean success = departmentService.updateEntity(entity);
if(!success){
return new JsonResult(Status.FAIL_OPERATION);
}
return new JsonResult(Status.OK);
return super.updateEntity(entity, result);
}
/***