异常消息处理
模拟案例
编写UserController2.java
save方法
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-36x48ttv-1651988258465)(springboot.assets/image-20220508125044686.png)]](https://img-blog.csdnimg.cn/27f63b2269ef4f6fa1b13964b8e0f4f3.png)
postman测试
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UOSLkVrC-1651988258466)(springboot.assets/image-20220508125407305.png)]](https://img-blog.csdnimg.cn/ae6d8d101d3e46fca4fac2463106c0a7.png)
小结
我们需要对异常数据做一个统一的处理,不然前端无法处理
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KEYIanue-1651988258467)(springboot.assets/image-20220508125436724.png)]](https://img-blog.csdnimg.cn/8f9c0ef63faf45788b077365c7b67713.png)
优化方案
编写切面工具
package com.taotao.controller.utils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@SuppressWarnings({"all"})
@ControllerAdvice
@RestControllerAdvice
public class ProjectExceptionAdvice {
@ExceptionHandler
public Result doExceptionAdvice(Exception ex){
ex.printStackTrace();
return new Result("服务器故障,请稍后再试");
}
}
给Result.java编写一个构造方法,用于返回数据
private String msg;
public Result(String msg){
this.flag = false;
this.msg = msg;
}
postman测试
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ij0CDZTu-1651988258468)(springboot.assets/image-20220508130834063.png)]](https://img-blog.csdnimg.cn/2f41e41b64fb4601bc1eea35ebd1afb2.png)
完成前端页面异常显示
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-y22s8WNv-1651988258469)(springboot.assets/image-20220508131515388.png)]](https://img-blog.csdnimg.cn/fcd8db389066487999755b61a7bfc84c.png)
测试成功
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qt5uGEnq-1651988258470)(springboot.assets/image-20220508131535823.png)]](https://img-blog.csdnimg.cn/75e5550e9c0246e6a6d0edfc38f6b4c7.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LYXrFUIA-1651988258471)(springboot.assets/image-20220508131437559.png)]](https://img-blog.csdnimg.cn/6cc7ad2cd20b400690299de85eb7e7d7.png)
优化方案2
改前端页面
不管成功还是失败我们都返回res.data.msg,逻辑我们都写在controller类里
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0FFFHF2s-1651988258471)(springboot.assets/image-20220508132927886.png)]](https://img-blog.csdnimg.cn/3804d6cc59e64f958a91034ac4c5328f.png)
编写UserController2.java
如果flag为true我们显示“添加成功”,否则显示“添加失败”
如果异常了(鬼鬼.equals(user.getUsername()),就报异常;
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cfwxht9t-1651988258472)(springboot.assets/image-20220508132854606.png)]](https://img-blog.csdnimg.cn/f99aefb6e8aa4b118ee60c366132de28.png)
记得添加一个构造方法
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YuUuZyQY-1651988258472)(springboot.assets/image-20220508133459319.png)]](https://img-blog.csdnimg.cn/05a34d7b602a4d558ce5ae514d657645.png)
测试成功
运行异常成功
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DqnWj2XS-1651988258473)(springboot.assets/image-20220508133027097.png)]](https://img-blog.csdnimg.cn/8d6962d69baa4d12bd53ab175d21e927.png)
姓名等于“鬼鬼”,异常

添加成功
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1wtEYR4X-1651988258473)(springboot.assets/image-20220508133125309.png)]](https://img-blog.csdnimg.cn/051c39fe00694c5a9437305a71ad0be6.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-65fApPqe-1651988258474)(springboot.assets/image-20220508133427086.png)]](https://img-blog.csdnimg.cn/8f2810189c944d12aa24f68c01e7c41e.png)
小结
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KUnOZmHZ-1651988258474)(springboot.assets/image-20220508133550286.png)]](https://img-blog.csdnimg.cn/05ab2bc4ba494a908027cb7396ce26e2.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MgWBf7Yr-1651988258475)(springboot.assets/image-20220508133603704.png)]](https://img-blog.csdnimg.cn/c71f2a55332f4705b2facdae59d32bfd.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lfd7GgSz-1651988258475)(springboot.assets/image-20220508133609728.png)]](https://img-blog.csdnimg.cn/e1f33178eb264b72a9d9d687f1976862.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LfE6uf6g-1651988258476)(springboot.assets/image-20220508133626650.png)]](https://img-blog.csdnimg.cn/4088dba90ad043f2a5eb6c0c5ce187fc.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OIgvsLNi-1651988258476)(springboot.assets/image-20220508133635645.png)]](https://img-blog.csdnimg.cn/7b302268ecc0458b9300df6973c6696b.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yqBfhSKx-1651988258477)(springboot.assets/image-20220508133644423.png)]](https://img-blog.csdnimg.cn/a30aacffdfe24a6b9f0dc4fa4fd11f93.png)
|