2.4 代码实现
在EmployeeController中增加save方法, 用于保存用户员工信息。
A. 在新增员工时, 按钮页面原型中的需求描述, 需要给员工设置初始默认密码 123456, 并对密码进行MD5加密。
B. 在组装员工信息时, 还需要封装创建时间、修改时间,创建人、修改人信息(从session中获取当前登录用户)。
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#aa5500">/**</span> <span style="color:#aa5500">* 新增员工</span> <span style="color:#aa5500">* @param employee</span> <span style="color:#aa5500">* @return</span> <span style="color:#aa5500">*/</span> <span style="color:#555555">@PostMapping</span> <span style="color:#770088">public</span> <span style="color:#000000">R</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#0000ff">save</span>(<span style="color:#000000">HttpServletRequest</span> <span style="color:#000000">request</span>,<span style="color:#555555">@RequestBody</span> <span style="color:#000000">Employee</span> <span style="color:#000000">employee</span>){ <span style="color:#000000">log</span>.<span style="color:#000000">info</span>(<span style="color:#aa1111">"新增员工,员工信息:{}"</span>,<span style="color:#000000">employee</span>.<span style="color:#000000">toString</span>()); <span style="color:#aa5500">//设置初始密码123456,需要进行md5加密处理</span> <span style="color:#000000">employee</span>.<span style="color:#000000">setPassword</span>(<span style="color:#000000">DigestUtils</span>.<span style="color:#000000">md5DigestAsHex</span>(<span style="color:#aa1111">"123456"</span>.<span style="color:#000000">getBytes</span>())); <span style="color:#000000">employee</span>.<span style="color:#000000">setCreateTime</span>(<span style="color:#000000">LocalDateTime</span>.<span style="color:#000000">now</span>()); <span style="color:#000000">employee</span>.<span style="color:#000000">setUpdateTime</span>(<span style="color:#000000">LocalDateTime</span>.<span style="color:#000000">now</span>()); <span style="color:#aa5500">//获得当前登录用户的id</span> <span style="color:#008855">Long</span> <span style="color:#000000">empId</span> <span style="color:#981a1a">=</span> (<span style="color:#008855">Long</span>) <span style="color:#000000">request</span>.<span style="color:#000000">getSession</span>().<span style="color:#000000">getAttribute</span>(<span style="color:#aa1111">"employee"</span>); <span style="color:#000000">employee</span>.<span style="color:#000000">setCreateUser</span>(<span style="color:#000000">empId</span>); <span style="color:#000000">employee</span>.<span style="color:#000000">setUpdateUser</span>(<span style="color:#000000">empId</span>); <span style="color:#000000">employeeService</span>.<span style="color:#000000">save</span>(<span style="color:#000000">employee</span>); <span style="color:#770088">return</span> <span style="color:#000000">R</span>.<span style="color:#000000">success</span>(<span style="color:#aa1111">"新增员工成功"</span>); }</span></span>
2.5 功能测试
代码编写完毕之后,我们需要将工程重启, 完毕之后直接访问管理系统首页, 点击 "员工管理" 页面中的 "添加员工" 按钮, 输入员工基本信息, 然后点击 "保存" 进行数据保存, 保存完毕后, 检查数据库中是否录入员工数据。
当我们在测试中,添加用户时, 输入了一个已存在的用户名时,前端界面出现错误提示信息:
而此时,服务端已经报错了, 报错信息如下:
出现上述的错误, 主要就是因为在 employee 表结构中,我们针对于username字段,建立了唯一索引,添加重复的username数据时,违背该约束,就会报错。但是此时前端提示的信息并不具体,用户并不知道是因为什么原因造成的该异常,我们需要给用户提示详细的错误信息 。
2.6 全局异常处理
2.6.1 思路分析
要想解决上述测试中存在的问题,我们需要对程序中可能出现的异常进行捕获,通常有两种处理方式:
A. 在Controller方法中加入 try...catch 进行异常捕获
形式如下:
如果采用这种方式,虽然可以解决,但是存在弊端,需要我们在保存其他业务数据时,也需要在Controller方法中加上try...catch进行处理,代码冗余,不通用。
B. 使用异常处理器进行全局异常捕获
采用这种方式来实现,我们只需要在项目中定义一个通用的全局异常处理器,就可以解决本项目的所有异常。
2.6.2 全局异常处理器
在项目中自定义一个全局异常处理器,在异常处理器上加上注解 @ControllerAdvice,可以通过属性annotations指定拦截哪一类的Controller方法。 并在异常处理器的方法上加上注解 @ExceptionHandler 来指定拦截的是那一类型的异常。
<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#770088">import</span> <span style="color:#000000">lombok</span>.<span style="color:#000000">extern</span>.<span style="color:#000000">slf4j</span>.<span style="color:#000000">Slf4j</span>; <span style="color:#770088">import</span> <span style="color:#000000">org</span>.<span style="color:#000000">springframework</span>.<span style="color:#000000">stereotype</span>.<span style="color:#000000">Controller</span>; <span style="color:#770088">import</span> <span style="color:#000000">org</span>.<span style="color:#000000">springframework</span>.<span style="color:#000000">web</span>.<span style="color:#000000">bind</span>.<span style="color:#000000">annotation</span>.<span style="color:#000000">ControllerAdvice</span>; <span style="color:#770088">import</span> <span style="color:#000000">org</span>.<span style="color:#000000">springframework</span>.<span style="color:#000000">web</span>.<span style="color:#000000">bind</span>.<span style="color:#000000">annotation</span>.<span style="color:#000000">ExceptionHandler</span>; <span style="color:#770088">import</span> <span style="color:#000000">org</span>.<span style="color:#000000">springframework</span>.<span style="color:#000000">web</span>.<span style="color:#000000">bind</span>.<span style="color:#000000">annotation</span>.<span style="color:#000000">ResponseBody</span>; <span style="color:#770088">import</span> <span style="color:#000000">org</span>.<span style="color:#000000">springframework</span>.<span style="color:#000000">web</span>.<span style="color:#000000">bind</span>.<span style="color:#000000">annotation</span>.<span style="color:#000000">RestController</span>; <span style="color:#770088">import</span> <span style="color:#000000">java</span>.<span style="color:#000000">sql</span>.<span style="color:#000000">SQLIntegrityConstraintViolationException</span>; <span style="color:#aa5500">/**</span> <span style="color:#aa5500">* 全局异常处理</span> <span style="color:#aa5500">*/</span> <span style="color:#555555">@ControllerAdvice</span>(<span style="color:#000000">annotations</span> <span style="color:#981a1a">=</span> {<span style="color:#000000">RestController</span>.<span style="color:#770088">class</span>, <span style="color:#000000">Controller</span>.<span style="color:#770088">class</span>}) <span style="color:#555555">@ResponseBody</span> <span style="color:#555555">@Slf4j</span> <span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">GlobalExceptionHandler</span> { <span style="color:#aa5500">/**</span> <span style="color:#aa5500">* 异常处理方法</span> <span style="color:#aa5500">* @return</span> <span style="color:#aa5500">*/</span> <span style="color:#555555">@ExceptionHandler</span>(<span style="color:#000000">SQLIntegrityConstraintViolationException</span>.<span style="color:#770088">class</span>) <span style="color:#770088">public</span> <span style="color:#000000">R</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#000000">exceptionHandler</span>(<span style="color:#000000">SQLIntegrityConstraintViolationException</span> <span style="color:#000000">ex</span>){ <span style="color:#000000">log</span>.<span style="color:#000000">error</span>(<span style="color:#000000">ex</span>.<span style="color:#000000">getMessage</span>()); <span style="color:#770088">if</span>(<span style="color:#000000">ex</span>.<span style="color:#000000">getMessage</span>().<span style="color:#000000">contains</span>(<span style="color:#aa1111">"Duplicate entry"</span>)){ <span style="color:#008855">String</span>[] <span style="color:#000000">split</span> <span style="color:#981a1a">=</span> <span style="color:#000000">ex</span>.<span style="color:#000000">getMessage</span>().<span style="color:#000000">split</span>(<span style="color:#aa1111">" "</span>); <span style="color:#008855">String</span> <span style="color:#000000">msg</span> <span style="color:#981a1a">=</span> <span style="color:#000000">split</span>[<span style="color:#116644">2</span>] <span style="color:#981a1a">+</span> <span style="color:#aa1111">"已存在"</span>; <span style="color:#770088">return</span> <span style="color:#000000">R</span>.<span style="color:#000000">error</span>(<span style="color:#000000">msg</span>); } <span style="color:#770088">return</span> <span style="color:#000000">R</span>.<span style="color:#000000">error</span>(<span style="color:#aa1111">"未知错误"</span>); } }</span></span>
2.6.3 测试
全局异常处理器编写完毕之后,我们需要将项目重启, 完毕之后直接访问管理系统首页, 点击 "员工管理" 页面中的 "添加员工" 按钮。当我们在测试中,添加用户时, 输入了一个已存在的用户名时,前端界面出现如下错误提示信息: