目录
1.前端触发异常
输入名称超过50个字符,提示框显示不能超过50个字符,异常前后端都会做出什么反应
首先F12检查可以看到返回的响应
Code为500
表单中岗位Name不能为空,不为空required则为true传数据到后端,后端处理,如果为空则required为false则不传入后端,这里我们不是为空则后端会接收处理
rules: {
postName: [
{ required: true, message: "岗位名称不能为空", trigger: "blur" }
],
postCode: [
{ required: true, message: "岗位编码不能为空", trigger: "blur" }
],
postSort: [
{ required: true, message: "岗位顺序不能为空", trigger: "blur" }
]
}
点击提交按钮后处理的函数
submitForm: function() {
this.$refs["form"].validate(valid => {
if (valid) {
if (this.form.postId != undefined) {
updatePost(this.form).then(response => {
this.$modal.msgSuccess("修改成功");
this.open = false;
this.getList();
});
} else {
addPost(this.form).then(response => {
this.$modal.msgSuccess("新增成功");
this.open = false;
this.getList();
});
}
}
});
}
前端传入后端
export function updatePost(data) {
return request({
url: '/system/post',
method: 'put',
data: data
})
}
后端处理
后端接口接收处理
@PreAuthorize("@ss.hasPermi('system:post:edit')")
@Log(title = "岗位管理", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult edit(@Validated @RequestBody SysPost post)
{
if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post)))
{
return AjaxResult.error("修改岗位'" + post.getPostName() + "'失败,岗位名称已存在");
}
else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post)))
{
return AjaxResult.error("修改岗位'" + post.getPostName() + "'失败,岗位编码已存在");
}
post.setUpdateBy(getUsername());
return toAjax(postService.updatePost(post));
}
进入SysPost中可以看到对官位名的注解
一是不能为空,二是不是超过50(这里不能为空注解基本不是被调用,因为上面解释如果输入为空则前端的required为false,就不会传入后端了,这里写注解是为了保险起见)
@NotBlank(message = "岗位名称不能为空")
@Size(min = 0, max = 50, message = "岗位名称长度不能超过50个字符")
public String getPostName()
{
return postName;
}
注册异常全局处理类
捕捉异常
在Run中也有显示全局异常处理类以及显示的捕捉异常的类型的函数,