1、测试数据

public class Test {

    public static void main(String[] args){

        System.out.println("Response.success(ErrorEnum.SUCCESS) = " + Response.success(ErrorEnum.SUCCESS));

        System.out.println("Response.success(ErrorEnum.FAIL) = " + Response.fail(ErrorEnum.FAIL));

        System.out.println("Response.fail() = " + Response.fail());

        System.out.println("Response.success() = " + Response.success());

    }

}

//Response.success(ErrorEnum.SUCCESS) = Response{code=200, msg='请求成功!', model=null}
//Response.success(ErrorEnum.FAIL) = Response{code=500, msg='请求失败!', model=null}
//Response.fail() = Response{code=500, msg='请求失败!', model=null}
//Response.success() = Response{code=200, msg='请求成功!', model=null}

 

2、创建Response类

public class Response<T> {

    private Integer code;

    private String msg;

    private T model;

    private volatile static Response response = null;

    private Response(){}

    public static Response getInstance(){
if (response == null){
synchronized (Response.class){
if (response == null){
response =new Response();
}
}
}
return response;
} public static Response fail(){
Response response = Response.getInstance();
response.setCode(ErrorEnum.FAIL.getCode());
response.setMsg(ErrorEnum.FAIL.getMsg());
return response;
} public static Response fail(ErrorEnum errorEnum){
Response response = Response.getInstance();
response.setCode(errorEnum.getCode());
response.setMsg(errorEnum.getMsg());
return response;
} public static <T> Response<T> fail(T data){
Response<T> response = Response.getInstance();
response.setCode(ErrorEnum.FAIL.getCode());
response.setMsg(ErrorEnum.FAIL.getMsg());
response.setModel(data);
return response;
} public static Response fail(int code, String msg) {
Response response = Response.getInstance();
response.setCode(code);
response.setMsg(msg);
return response;
} public static Response success(){
Response response = Response.getInstance();
response.setCode(ErrorEnum.SUCCESS.getCode());
response.setMsg(ErrorEnum.SUCCESS.getMsg());
return response;
} public static Response success(ErrorEnum errorEnum){
Response response = Response.getInstance();
response.setCode(errorEnum.getCode());
response.setMsg(errorEnum.getMsg());
return response;
} public static <T> Response<T> success(T data){
Response<T> response = Response.getInstance();
response.setCode(ErrorEnum.SUCCESS.getCode());
response.setMsg(ErrorEnum.SUCCESS.getMsg());
response.setModel(data);
return response;
} public Integer getCode() {
return code;
} public void setCode(Integer code) {
this.code = code;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
} public T getModel() {
return model;
} public void setModel(T model) {
this.model = model;
} @Override
public String toString() {
return "Response{" +
"code=" + code +
", msg='" + msg + '\'' +
", model=" + model +
'}';
}
}

封装统一请求状态返回Result的更多相关文章

  1. uni-app中封装统一请求函数

    封装统一请求函数有利于项目的维护 整体功能简单实用,但小编遇到一个巨坑,项目中在vue文件使用跳转方法,url参数输入 "/" 后工具提示的路径为 "/pages/log ...

  2. FastAPI 学习之路(五十九)封装统一的json返回处理工具

    这之前的接口,我们返回的格式都是每个接口异常返回的数据格式都会不一样,我们处理起来没有那么方便,我们可以封装一个统一的json处理. 那么我们看下如何来实现呢 from fastapi import ...

  3. WebService如何封装XML请求 以及解析接口返回的XML

    原 WebService如何封装XML请求 以及解析接口返回的XML 置顶 2019年08月16日 15:00:47 童子泛舟 阅读数 28 标签: XML解析WebService第三方API 更多 ...

  4. http statusCode(状态码)请求URL返回状态值的含义

    http statusCode(状态码)请求URL返回状态值的含义 请求URL浏览器返回状态码的含义(http statusCode): 201-206都表示服务器成功处理了请求的状态代码,说明网页可 ...

  5. JavaWeb-用过滤器修改请求的返回状态码

    问题: 客户SDK对接服务,默认只识别200和非200的请求状态码.需要修改当前应用的状态码(如将201转为200) 解决方案:通过扩展HttpServletResponseWrapper,获取到每个 ...

  6. WebApi系列~基于单请求封装多请求的设计

    回到目录 怎么说,单请求封装多请求,这句话确实有点绕了,但还是要看清楚,想明白这到底是怎么一回事,单请求即一次请求(get,post,put,delete),封闭多请求,即在客户端发送的一个请求中可能 ...

  7. 微信小程序开发——使用promise封装异步请求

    前言: 有在学vue的网友问如何封装网络请求,这里以正在写的小程序为例,做一个小程序的请求封装. 关于小程序发起 HTTPS 网络请求的Api,详情可以参考官方文档:wx.request(Object ...

  8. webapi基于单请求封装多请求的设计【转】

    怎么说,单请求封装多请求,这句话确实有点绕了,但还是要看清楚,想明白这到底是怎么一回事,单请求即一次请求(get,post,put,delete),封闭多请求,即在客户端发送的一个请求中可能包含多个子 ...

  9. Spring Boot 无侵入式 实现RESTful API接口统一JSON格式返回

    前言 现在我们做项目基本上中大型项目都是选择前后端分离,前后端分离已经成了一个趋势了,所以总这样·我们就要和前端约定统一的api 接口返回json 格式, 这样我们需要封装一个统一通用全局 模版api ...

  10. 基于.NetCore开发博客项目 StarBlog - (24) 统一接口数据返回格式

    前言 开发接口,是给客户端(Web前端.App)用的,前面说的RESTFul,是接口的规范,有了统一的接口风格,客户端开发人员在访问后端功能的时候能更快找到需要的接口,能写出可维护性更高的代码. 而接 ...

随机推荐

  1. Abp学习(一) abp+vue +mysql框架搭建

    一.到Abp官网下载框架 地址:https://aspnetboilerplate.com/Templates 二.打开项目 修改数据库连接为MySql,默认是SQL Server 2.1.修改链接字 ...

  2. redis单机搭建

    1.获取redis wget http://download.redis.io/releases/redis-6.2.7.tar.gz tar -xvf redis-6.2.7.tar.gz 2.安装 ...

  3. Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'hive.DELETEME1643159643943' doesn't exist

    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'hive.DELETEME1643159643 ...

  4. 删除Mac版word上方的Mathtype

    原因 Mac升级到macOS Catalina v10.15.3发现mathtype用不了, Mathtype官网说目前暂时不支持这个版本的系统. 现在尴尬的是, mathtype删除了, 但一不小心 ...

  5. Python中的startswith()函数用法

    函数:startswith() 作用:判断字符串是否以指定字符或子字符串开头 一.函数说明语法:string.startswith(str, beg=0,end=len(string))        ...

  6. git rebase时出现的提示信息

    root@host: /home/wkxnk/project$  git rebase master First, rewinding head to replay your work on top ...

  7. mmdetection加载ndarray数据,并训练

    1.构造coco数据集,file_name为具体的ndarray文件名,类名的改变和class_num的配置和之前一样.保存的npy文件是归一化之后的结果 2.修改数据加载代码,将 2.修改网络输入i ...

  8. 关于k8s微服务的基础知识分享总结

    1.说起k8s,先得讲讲微服务,来个图(百度上找到的图),初识 1.微服务架构强调的是一种架构模式,提倡将单一的应用程序,划分为一组小的服务,每个服务运行在其独立的自己的进程中,服务之间相互协调配合, ...

  9. MybatisX无法自动生成entity实体类

    在做项目的时候,安装MybatisX插件可以让我们不用写实体类,加快我们的开发速度,让我们更专注于业务逻辑的开发,可是最近在做项目的时候,发现MybatisX插件的MybatisX-Generator ...

  10. NET 8 预览版 2 亮点是Blazor

    .NET 团队在2023年3月14日发布了.NET 8预览版2,博客文章地址:https://devblogs.microsoft.com/dotnet/announcing-dotnet-8-pre ...