java web 通用返回json 统一返回结果

开源个自己封装的通用json返回吧,自我感觉写的还行…emmm…

主类:调用返回的

package com.orange.verify.adminweb.model;

/**
 * web专用返回
 * @author Orange
 * @date 2018/10/28
 */
public class Response {

    private int code;

    private String msg;

    private T data;

    private long totalTime;

    private Response(ResponseCode responseCode) {
        this.code = responseCode.getCode();
        this.msg = responseCode.getDesc();
    }

    private Response(ResponseCode responseCode,T data) {
        this.code = responseCode.getCode();
        this.msg = responseCode.getDesc();
        this.data = data;
    }

    private Response(ResponseCode responseCode,String msg) {
        this.code = responseCode.getCode();
        this.msg = msg;
    }

    private Response(ResponseCode responseCode,String msg,T data) {
        this.code = responseCode.getCode();
        this.msg = msg;
        this.data = data;
    }

    public static Response success() {
        return new Response(ResponseCode.SUCCESS);
    }
    public static Response error() {
        return new Response(ResponseCode.ERROR);
    }
    public static Response build(ResponseCode responseCode) {
        return new Response(responseCode);
    }
    public static Response build(ResponseCode responseCode,T data) {
        return new Response(responseCode,data);
    }
    public static Response build(ResponseCode responseCode,String msg) {
        return new Response(responseCode,msg);
    }
    public static Response build(ResponseCode responseCode,String msg,T data) {
        return new Response(responseCode,msg,data);
    }

    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;
    }

    public long getTotalTime() {
        return totalTime;
    }

    public void setTotalTime(long totalTime) {
        this.totalTime = totalTime;
    }
}

code码管理枚举类

package com.orange.verify.adminweb.model;

public enum ResponseCode {

    SUCCESS(10,"操作成功"),
    ERROR(11,"操作失败"),

    NOT_ROLE(13,"无角色"),

    NOT_LOGIN(12,"未登录");

    private int code;
    private String desc;

    ResponseCode(int code,String desc){
        this.code = code;
        this.desc = desc;
    }

    public int getCode(){
        return code;
    }

    public String getDesc(){
        return desc;
    }

}

测试代码

package com.orange.verify.adminweb.model;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

public class Test {

    public static void main(String[] args) {

        Response success = Response.success();
        System.out.println(JSON.toJSON(success));

        Response error = Response.error();
        System.out.println(JSON.toJSON(error));

        List data = new ArrayList<>();
        data.add("23424");
        Response> build = Response.build(ResponseCode.ERROR, data);
        System.out.println(JSON.toJSON(build));

        Response build1 = Response.build(ResponseCode.ERROR, "3333333");
        System.out.println(JSON.toJSON(build1));

        Response build2 = Response.build(ResponseCode.SUCCESS);
        System.out.println(JSON.toJSON(build2));

        Response> build3 = Response.build(ResponseCode.ERROR, "32423423", data);
        System.out.println(JSON.toJSON(build3));

    }

}

 
  

结果:
java web 通用返回json 统一返回结果_第1张图片

你可能感兴趣的:(JAVA)