SpringBoot返回结果统一处理实例详解

 更新时间:2023年12月10日 17:33:05   作者:wx59bcc77095d22  
这篇文章主要为大家介绍了SpringBoot返回结果统一处理实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

一、前言

在Web开发中,我们常常需要对API接口的返回结果进行统一的包装,以方便客户端对数据和异常情况的统一处理。我们可以自定义返回接口结果包装类。

二、创建返回结果枚举类

package com.example.hellodemo.enums;
/**
 * @author qx
 * @date 2023/11/30
 * @des 返回结果枚举类
 */
public enum ResultTypeEnum {
    SUCCESS(0, "成功"), FAILURE(1, "失败");
    private final int code;
    private final String msg;
    ResultTypeEnum(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public int getCode() {
        return code;
    }
    public String getMsg() {
        return msg;
    }
}

三、定义统一返回结果类

package com.example.hellodemo.bean;
import com.example.hellodemo.enums.ResultTypeEnum;
import java.io.Serializable;
/**
 * @author qx
 * @date 2023/11/30
 * @des 统一返回结果类
 */
public class ResultResponse<T> implements Serializable {
    private int code;
    private String msg;
    private T data;
    public ResultResponse(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    public static <T> ResultResponse success() {
        return new ResultResponse(ResultTypeEnum.SUCCESS.getCode(), ResultTypeEnum.SUCCESS.getMsg(), null);
    }
    public static <T> ResultResponse success(T data) {
        return new ResultResponse(ResultTypeEnum.SUCCESS.getCode(), ResultTypeEnum.SUCCESS.getMsg(), data);
    }
    public static <T> ResultResponse success(String msg, T data) {
        return new ResultResponse(ResultTypeEnum.SUCCESS.getCode(), msg, data);
    }
    public static ResultResponse failure() {
        return new ResultResponse(ResultTypeEnum.FAILURE.getCode(), ResultTypeEnum.FAILURE.getMsg(), null);
    }
    public static ResultResponse failure(String msg) {
        return new ResultResponse(ResultTypeEnum.FAILURE.getCode(), msg, null);
    }
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public T getData() {
        return data;
    }
    public void setData(T data) {
        this.data = data;
    }
}

四、创建控制器

package com.example.hellodemo.controller;
import com.example.hellodemo.bean.ResultResponse;
import com.example.hellodemo.bean.one.DbOneEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
/**
 * @author qx
 * @date 2023/11/30
 * @des
 */
@RestController
public class DbController {
    @PostMapping("/test")
    public ResultResponse<Map<String, Object>> testDemo(@RequestParam String name, @RequestParam Integer id) {
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", name);
        hashMap.put("id", id);
        return ResultResponse.success(hashMap);
    }
}

测试:

我们也可以返回自定义的msg。

package com.example.hellodemo.controller;
import com.example.hellodemo.bean.ResultResponse;
import com.example.hellodemo.bean.one.DbOneEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
/**
 * @author qx
 * @date 2023/11/30
 * @des
 */
@RestController
public class DbController {
    @PostMapping("/test")
    public ResultResponse<Map<String, Object>> testDemo(@RequestParam String name, @RequestParam Integer id) {
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", name);
        hashMap.put("id", id);
        return ResultResponse.success("获取数据成功",hashMap);
    }
}

测试:

五、全局异常统一返回类

package com.example.hellodemo.exception;
import com.example.hellodemo.bean.ResultResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
 * @author qx
 * @date 2023/11/30
 * @des 全局异常统一返回处理类
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 捕获Exceptino异常类型
     *
     * @param e
     * @return 返回异常统一返回处理结果
     */
    @ExceptionHandler(value = {Exception.class})
    public ResultResponse exceptionHandler(Exception e) {
        return ResultResponse.failure(e.getMessage());
    }
}

我们在控制器中自己创建一个异常,然后请求接口,看看返回什么?

package com.example.hellodemo.controller;
import com.example.hellodemo.bean.ResultResponse;
import com.example.hellodemo.bean.one.DbOneEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
/**
 * @author qx
 * @date 2023/11/30
 * @des
 */
@RestController
public class DbController {
    @PostMapping("/test")
    public ResultResponse<Map<String, Object>> testDemo(@RequestParam String name, @RequestParam Integer id) {
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", name);
        hashMap.put("id", id);
        // 自己写一个异常
        int i = 1 / 0;
        return ResultResponse.success("获取数据成功", hashMap);
    }
}

测试:

我们看到msg已经返回了异常的相关信息。

六、Spring切面实现自动返回统一结果

package com.example.hellodemo.config;
import com.example.hellodemo.bean.ResultResponse;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
/**
 * @author qx
 * @date 2023/11/30
 * @des 全局统一返回结果
 */
@RestControllerAdvice
public class GlobalResponseAdvice implements ResponseBodyAdvice<Object> {
    @Override
    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
        return true;
    }
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        // 要排除的api,里面的接口不需要统一包装
        String[] excludePath = {};
        for (String path : excludePath) {
            if (serverHttpRequest.getURI().getPath().startsWith(path)) {
                return body;
            }
        }
        if (body instanceof ResultResponse) {
            return body;
        }
        return ResultResponse.success(body);
    }
}

我们定义一个不带返回格式的控制器。

package com.example.hellodemo.controller;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
/**
 * @author qx
 * @date 2023/11/30
 * @des
 */
@RestController
public class DbController {
    @PostMapping("/test")
    public Map<String, Object> testDemo(@RequestParam String name, @RequestParam Integer id) {
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", name);
        hashMap.put("id", id);
        return hashMap;
    }
}

我们进行测试:

我们发现我们使用切面设置统一返回结果的封装成功了,这样就统一了返回格式,对代码没有侵入。

我们注意全局异常统一返回和切面使用统一返回结果都使用了一个注解@RestControllerAdvice。

以上就是SpringBoot返回结果统一处理实例详解的详细内容,更多关于SpringBoot返回结果统一处理的资料请关注脚本之家其它相关文章!

相关文章

  • sun unsafe类功能及使用注意事项详解

    sun unsafe类功能及使用注意事项详解

    这篇文章主要为大家介绍了unsafe类的功能及在使用中需要注意的事项详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-01-01
  • java  ThreadPoolExecutor使用方法简单介绍

    java ThreadPoolExecutor使用方法简单介绍

    这篇文章主要介绍了java ThreadPoolExecutor使用方法简单介绍的相关资料,需要的朋友可以参考下
    2017-02-02
  • Java基于IO流实现登录和注册功能

    Java基于IO流实现登录和注册功能

    这篇文章主要为大家详细介绍了Java基于IO流实现登录和注册功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • 基于swing实现窗体拖拽和拉伸

    基于swing实现窗体拖拽和拉伸

    这篇文章主要为大家详细介绍了基于swing实现窗体拖拽和拉伸,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • jmeter正则表达式的使用

    jmeter正则表达式的使用

    在jmeter中,可以利用正则表达式提取器来帮助我们完成这一动作,本文就详细的介绍一下应该如何使用,感兴趣的可以了解一下
    2021-11-11
  • SpringCloud Feign客户端使用流程

    SpringCloud Feign客户端使用流程

    在springcloud中,openfeign是取代了feign作为负载均衡组件的,feign最早是netflix提供的,他是一个轻量级的支持RESTful的http服务调用框架,内置了ribbon,而ribbon可以提供负载均衡机制,因此feign可以作为一个负载均衡的远程服务调用框架使用
    2023-01-01
  • Java interrupt()方法使用注意_动力节点Java学院整理

    Java interrupt()方法使用注意_动力节点Java学院整理

    这篇文章主要介绍了Java interrupt()方法使用注意_动力节点Java学院整理,需要的朋友可以参考下
    2017-05-05
  • SpringBoot学习之基于注解的缓存

    SpringBoot学习之基于注解的缓存

    spring boot对缓存支持非常灵活,我们可以使用默认的EhCache,也可以整合第三方的框架,只需配置即可,下面这篇文章主要给大家介绍了关于SpringBoot学习之基于注解缓存的相关资料,需要的朋友可以参考下
    2022-03-03
  • Java通过stmp协议发送邮件

    Java通过stmp协议发送邮件

    这篇文章主要为大家详细介绍了Java通过stmp协议发送邮件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • Java IO流之原理分类与节点流文件操作详解

    Java IO流之原理分类与节点流文件操作详解

    流(Stream)是指一连串的数据(字符或字节),是以先进先出的方式发送信息的通道,数据源发送的数据经过这个通道到达目的地,按流向区分为输入流和输出流
    2021-10-10

最新评论