首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >五、从0搭建Springboot工程

五、从0搭建Springboot工程

作者头像
程序员三明治
发布2025-12-18 19:56:22
发布2025-12-18 19:56:22
730
举报
文章被收录于专栏:码力up码力up

统一返回包装类

统一返回包装类,作用是统一后端返回的数据类型,你别一会儿是String,一会Integer,一会Map的形式,统一处理

代码语言:javascript
复制
public class Result {

    private String code;
    private String msg;
    private Object data;

    // 无数据返回
    public static Result success() {
        Result res = new Result();
        res.setCode("200");
        res.setMsg("成功");
        return res;
    }
    // 有数据返回
    public static Result success(Object data) {
        Result res = new Result();
        res.setCode("200");
        res.setMsg("成功");
        res.setData(data);
        return res;
    }

    public static Result error() {
        Result res = new Result();
        res.setCode("500");
        res.setMsg("错误");
        return res;
    }

    public static Result error(String code, String msg) {
        Result res = new Result();
        res.setCode(code);
        res.setMsg(msg);
        return res;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}

全局异常处理

@ControllerAdvice标识一个全局异常处理的类,当应用程序中的Controller出现异常时,可以有一个集中的地方来处理这些异常,而不是在每个Controller中单独处理 

代码语言:javascript
复制
package org.example.exception;

import org.example.common.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice("org.example.controller")
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class) // 只要是exception及下面的子类都会捕获
    @ResponseBody //返回json串
    public Result error(Exception e) {
        e.printStackTrace(); //在控制台上打印错误信息
        return Result.error();
    }

    @ExceptionHandler(CustomException.class) // 捕捉抛出的自定义异常
    @ResponseBody //返回json串
    public Result error(CustomException e) {
        return Result.error(e.getCode(), e.getMsg());
    }
}

自定义异常

自定义异常允许开发者定义特定的错误类型,便于在代码中精确捕获和处理。

 异常的继承关系

代码语言:javascript
复制
package org.example.exception;

/**
 * @description 自定义异常
 */
public class CustomException extends RuntimeException{

    private String code;
    private String msg;

    public CustomException(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

WebController

此controller一般用于写用户处理登录注册等和业务不相关的接口

代码语言:javascript
复制
package org.example.controller;


import org.example.common.Result;
import org.example.exception.CustomException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
public class WebController {

    @GetMapping("/hello")
    public Result hello() {
        return Result.success("hello world");
    }
    @GetMapping("/count")
    public Result count() {
        throw new CustomException("400", "错误!禁止请求");
//        return Result.success(10);
    }
    @GetMapping("/count1")
    public Result count1() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("count", "三明治");
        map.put("age", 20);
        return Result.success(map);
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-12-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 统一返回包装类
  • 全局异常处理
  • 自定义异常
  • WebController
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档