springboot + 统一返回json数据 + 统一异常处理

迈不过友情╰ 2024-03-16 10:26 183阅读 0赞

环境

1、创建一个springboot项目

  • 只需引入web依赖即可

在这里插入图片描述

  • pom.xml文件



    org.springframework.boot
    spring-boot-starter-web

2、简单的配置

  1. # 应用名称
  2. spring.application.name=res_exc_demo
  3. # 端口号
  4. server.port=8099

统一返回

1、定义一个类,返回固定的格式

一般格式包括

  • 状态码
  • 状态码信息
  • 具体返回数据

    public class Resp {

  1. //状态码
  2. private int code = 200;
  3. //状态码信息
  4. private String msg = "success";
  5. //返回的信息
  6. private T data;
  7. private Resp(int code,String msg,T data){
  8. this.code = code;
  9. this.msg = msg;
  10. this.data = data;
  11. }
  12. public static <T> Resp success(T data){
  13. Resp resp = new Resp(200, "success", data);
  14. return resp;
  15. }
  16. public static <T> Resp success(String msg,T data){
  17. Resp resp = new Resp(200,msg, data);
  18. return resp;
  19. }
  20. public static <T> Resp error(AppExceptionCodeMsg appExceptionCodeMsg){
  21. Resp resp = new Resp(appExceptionCodeMsg.getCode(), appExceptionCodeMsg.getMsg(), null);
  22. return resp;
  23. }
  24. public static <T> Resp error(int code,String msg){
  25. Resp resp = new Resp(code,msg, null);
  26. return resp;
  27. }
  28. public int getCode() {
  29. return code;
  30. }
  31. public String getMsg() {
  32. return msg;
  33. }
  34. public T getData() {
  35. return data;
  36. }
  37. }

2、controller统一返回

  1. @RestController
  2. public class DemoController {
  3. @GetMapping("demo")
  4. public Resp<String> demo1(String name){
  5. //...
  6. return Resp.success("default");
  7. }
  8. }

统一异常处理

1、创建一个枚举类型

存储各种类型的异常状态码和信息

  1. package com.pzz.exce;
  2. /**
  3. * 枚举类,所有自定义异常的状态码和信息
  4. */
  5. //这个枚举类中定义的都是跟业务有关的异常
  6. public enum AppExceptionCodeMsg {
  7. INVALID_CODE(10000,"验证码无效"),
  8. USERNAME_NOT_EXISTS(10001,"用户名不存在"),
  9. USER_CREDIT_NOT_ENOUTH(10002,"用户积分不足");
  10. private int code ;
  11. private String msg ;
  12. public int getCode() {
  13. return code;
  14. }
  15. public String getMsg() {
  16. return msg;
  17. }
  18. AppExceptionCodeMsg(int code, String msg){
  19. this.code = code;
  20. this.msg = msg;
  21. }
  22. }

2、创建自定义的异常类

  1. package com.pzz.exce;
  2. public class AppException extends RuntimeException{
  3. private int code = 500;
  4. private String msg = "服务器异常";
  5. public AppException(AppExceptionCodeMsg appExceptionCodeMsg){
  6. super();
  7. this.code = appExceptionCodeMsg.getCode();
  8. this.msg = appExceptionCodeMsg.getMsg();
  9. }
  10. public AppException(int code,String msg){
  11. super();
  12. this.code = code;
  13. this.msg = msg;
  14. }
  15. public int getCode() {
  16. return code;
  17. }
  18. public String getMsg() {
  19. return msg;
  20. }
  21. }

3、创建全局统一异常处理类

  1. package com.pzz.exce;
  2. import com.pzz.resp.Resp;
  3. import org.springframework.web.bind.annotation.ControllerAdvice;
  4. import org.springframework.web.bind.annotation.ExceptionHandler;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. /**
  7. * 全局异常
  8. */
  9. @ControllerAdvice
  10. public class GlobalExceptionHandler {
  11. @ExceptionHandler(value = {
  12. Exception.class})
  13. @ResponseBody
  14. public <T> Resp<T> exceptionHandler(Exception e){
  15. //这里先判断拦截到的Exception是不是我们自定义的异常类型
  16. if(e instanceof AppException){
  17. AppException appException = (AppException)e;
  18. return Resp.error(appException.getCode(),appException.getMsg());
  19. }
  20. //如果拦截的异常不是我们自定义的异常(例如:数据库主键冲突)
  21. return Resp.error(500,"服务器端异常");
  22. }
  23. }

4、controller

  1. package com.pzz.controller;
  2. import com.pzz.exce.AppException;
  3. import com.pzz.exce.AppExceptionCodeMsg;
  4. import com.pzz.resp.Resp;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. @RestController
  10. public class DemoController {
  11. @GetMapping("demo")
  12. public Resp<String> demo1(String name){
  13. if("ok".equals(name)){
  14. return Resp.success("succ");
  15. }
  16. if("err".equals(name)){
  17. //抛业务相关的异常
  18. throw new AppException(AppExceptionCodeMsg.USERNAME_NOT_EXISTS);
  19. }
  20. if("errcode".equals(name)){
  21. throw new AppException(AppExceptionCodeMsg.INVALID_CODE);
  22. }
  23. if("0".equals(name)){
  24. int i=1/0;
  25. }
  26. //检查用户积分是否足够,如果不够,就抛出异常
  27. if("notenough".equals(name)){
  28. throw new AppException(AppExceptionCodeMsg.USER_CREDIT_NOT_ENOUTH);
  29. }
  30. return Resp.success("default");
  31. }
  32. @GetMapping("list")
  33. public Resp<List> list(){
  34. List<String> list = Arrays.asList("zhangsan","lisi","wangwu");
  35. return Resp.success(list);
  36. }
  37. }

测试效果

在这里插入图片描述
在这里插入图片描述

结束!
hy:6


  1. 人可以犯错,但是不可犯同一个错。---苏格拉底

发表评论

表情:
评论列表 (有 0 条评论,183人围观)

还没有评论,来说两句吧...

相关阅读