Java 封装HTTP返回结果类

Posted 流楚丶格念

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 封装HTTP返回结果类相关的知识,希望对你有一定的参考价值。

文章目录

封装类如下

HTTP状态码枚举类

package com.yyl.model.common.enums;

public enum AppHttpCodeEnum 

    // 成功段固定为200
    SUCCESS(200,"操作成功"),
    // 登录段1~50
    NEED_LOGIN(1,"需要登录后操作"),
    LOGIN_PASSWORD_ERROR(2,"密码错误"),
    // TOKEN50~100
    TOKEN_INVALID(50,"无效的TOKEN"),
    TOKEN_EXPIRE(51,"TOKEN已过期"),
    TOKEN_REQUIRE(52,"TOKEN是必须的"),
    // SIGN验签 100~120
    SIGN_INVALID(100,"无效的SIGN"),
    SIG_TIMEOUT(101,"SIGN已过期"),
    // 参数错误 500~1000
    PARAM_REQUIRE(500,"缺少参数"),
    PARAM_INVALID(501,"无效参数"),
    PARAM_IMAGE_FORMAT_ERROR(502,"图片格式有误"),
    SERVER_ERROR(503,"服务器内部错误"),
    // 数据错误 1000~2000
    DATA_EXIST(1000,"数据已经存在"),
    AP_USER_DATA_NOT_EXIST(1001,"ApUser数据不存在"),
    DATA_NOT_EXIST(1002,"数据不存在"),
    // 数据错误 3000~3500
    NO_OPERATOR_AUTH(3000,"无权限操作"),
    NEED_ADMIND(3001,"需要管理员权限"),

    // 自媒体文章错误 3501~3600
    MATERIASL_REFERENCE_FAIL(3501,"素材引用失效");


    int code;
    String errorMessage;

    AppHttpCodeEnum(int code, String errorMessage)
        this.code = code;
        this.errorMessage = errorMessage;
    

    public int getCode() 
        return code;
    

    public String getErrorMessage() 
        return errorMessage;
    


通用的结果返回类

package com.yyl.model.common.dtos;

import com.alibaba.fastjson.JSON;
import com.heima.model.common.enums.AppHttpCodeEnum;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通用的结果返回类
 * @param <T>
 */
public class ResponseResult<T> implements Serializable 

    private String host;

    private Integer code;

    private String errorMessage;

    private T data;

    public ResponseResult() 
        this.code = 200;
    

    public ResponseResult(Integer code, T data) 
        this.code = code;
        this.data = data;
    

    public ResponseResult(Integer code, String msg, T data) 
        this.code = code;
        this.errorMessage = msg;
        this.data = data;
    

    public ResponseResult(Integer code, String msg) 
        this.code = code;
        this.errorMessage = msg;
    

    public static ResponseResult errorResult(int code, String msg) 
        ResponseResult result = new ResponseResult();
        return result.error(code, msg);
    

    public static ResponseResult okResult(int code, String msg) 
        ResponseResult result = new ResponseResult();
        return result.ok(code, null, msg);
    

    public static ResponseResult okResult(Object data) 
        ResponseResult result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        if(data!=null) 
            result.setData(data);
        
        return result;
    

    public static ResponseResult errorResult(AppHttpCodeEnum enums)
        return setAppHttpCodeEnum(enums,enums.getErrorMessage());
    

    public static ResponseResult errorResult(AppHttpCodeEnum enums, String errorMessage)
        return setAppHttpCodeEnum(enums,errorMessage);
    

    public static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums)
        return okResult(enums.getCode(),enums.getErrorMessage());
    

    private static ResponseResult setAppHttpCodeEnum(AppHttpCodeEnum enums, String errorMessage)
        return okResult(enums.getCode(),errorMessage);
    

    public ResponseResult<?> error(Integer code, String msg) 
        this.code = code;
        this.errorMessage = msg;
        return this;
    

    public ResponseResult<?> ok(Integer code, T data) 
        this.code = code;
        this.data = data;
        return this;
    

    public ResponseResult<?> ok(Integer code, T data, String msg) 
        this.code = code;
        this.data = data;
        this.errorMessage = msg;
        return this;
    

    public ResponseResult<?> ok(T data) 
        this.data = data;
        return this;
    

    public Integer getCode() 
        return code;
    

    public void setCode(Integer code) 
        this.code = code;
    

    public String getErrorMessage() 
        return errorMessage;
    

    public void setErrorMessage(String errorMessage) 
        this.errorMessage = errorMessage;
    

    public T getData() 
        return data;
    

    public void setData(T data) 
        this.data = data;
    

    public String getHost() 
        return host;
    

    public void setHost(String host) 
        this.host = host;
    

测试一下

写一个main方法在里面,测试各个功能:

import com.alibaba.fastjson.JSON;
import com.heima.model.common.enums.AppHttpCodeEnum;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
	业务功能......
*/

public static void main(String[] args) 
    //前置
    System.out.println("*********************  状态信息  *************************");
    System.out.println("成功========================");
    AppHttpCodeEnum success = AppHttpCodeEnum.SUCCESS;
    System.out.println(success.getCode());
    System.out.println(success.getErrorMessage());
    System.out.println("需要登陆========================");
    AppHttpCodeEnum login = AppHttpCodeEnum.NEED_LOGIN;
    System.out.println(login.getCode());
    System.out.println(login.getErrorMessage());

    //查询一个对象
    System.out.println("*********************  查询一个对象  *************************");
    Map map = new HashMap();
    map.put("name", "林若溪");
    map.put("age", 18);
    ResponseResult result = ResponseResult.okResult(map);
    System.out.println(JSON.toJSONString(result));


    //新增,修改,删除  在项目中统一返回成功即可
    System.out.println("*********************  新增,修改,删除   *************************");

    ResponseResult result1 = ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    System.out.println(JSON.toJSONString(result1));


    //根据不用的业务返回不同的提示信息  比如:当前操作需要登录、参数错误
    System.out.println("*********************  根据不用的业务返回不同的提示信息  *************************");
    ResponseResult result2 = ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN, "自定义提示信息:啦啦啦啦啦啦啦啦啦");
    System.out.println(JSON.toJSONString(result2));

    //查询分页信息
    System.out.println("*********************  查询分页信息  *************************");
    PageResponseResult responseResult = new PageResponseResult(1,2,10);
    List list = new ArrayList();
    list.add("玉雪凝");
    list.add("苏映雪");
    list.add("雪凝");
    list.add("苏雪");
    list.add("玉凝");
    list.add("苏映");
    responseResult.setData(list);
    System.out.println(JSON.toJSONString(responseResult));


运行结果如下

以上是关于Java 封装HTTP返回结果类的主要内容,如果未能解决你的问题,请参考以下文章

Java封装公共Result结果返回类

java返回分页结果集的封装

java中封装的jar,方法能够正常的调用,但是调用返回都没结果。

java对jdbc操作结果简单的映射封装

C语言或者C++如何调用一个http接口并得到返回结果?

Laravel通过request表单验证类返回封装http状态码