All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.javaclub.sword.web.HttpResult Maven / Gradle / Ivy

The newest version!
package com.github.javaclub.sword.web;

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

import com.github.javaclub.sword.core.BizObjects;
import com.github.javaclub.sword.core.Strings;
import com.github.javaclub.sword.domain.MessageCode;
import com.google.common.collect.Lists;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("Api返回")
public class HttpResult {
	
	public static final String MESSAGE_SUCCESS = "ok";
    public static final String MESSAGE_FAILURE = "error";
    
    public static final int RESPONSE_SUCCESS = 200;
    public static final int RESPONSE_FAILURE = 500;

	@ApiModelProperty(value = "请求是否成功", allowableValues = "true, false")
	protected boolean status;
	
	@ApiModelProperty(value = "业务响应码")
    protected String code = String.valueOf(RESPONSE_SUCCESS);
    
    @ApiModelProperty(value = "错误信息/提示文案信息")
    protected String message = MESSAGE_SUCCESS;
    
    @ApiModelProperty(value = "业务数据")
    protected T entry;
    
    @ApiModelProperty(value = "请求的traceId")
    private String traceId;
    
    /**
     * 异常信息(不提示给用户) 便于抓包排查问题,特别是未知系统异常,一般在debug模式下启用
     */
    @ApiModelProperty(value = "异常信息")
    private String exception = Strings.EMPTY_STRING;
    
    /**
     * 响应时间
     */
    @ApiModelProperty(value = "服务端执行耗时(ms)")
    private Long elapsedMillis = -1L;
    
    
    /**
	 * 额外扩展数据 
	 */
    @ApiModelProperty(value = "其他关联数据")
	private Map extra;

    public HttpResult() {
    	this.extra = new HashMap<>();
    }
    
    public HttpResult(String code, String message) {
    	this.code = code;
    	this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getCode() {
        return code;
    }

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

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public T getEntry() {
		return entry;
	}

	public void setEntry(T entry) {
		this.entry = entry;
	}
	
	public String getTraceId() {
		return traceId;
	}

	public void setTraceId(String traceId) {
		this.traceId = traceId;
	}

	public String getException() {
		return exception;
	}

	public void setException(String exception) {
		this.exception = exception;
	}

	public Long getElapsedMillis() {
		return elapsedMillis;
	}

	public void setElapsedMillis(Long elapsedMillis) {
		this.elapsedMillis = elapsedMillis;
	}

	public HttpResult addExtraData(String key, Object value) {
		if (null == extra) {
			extra = new HashMap();
		}
		extra.put(key, value);
		return this;
	}
	
	public Map getExtra() {
		return extra;
	}

	public void setExtra(Map extraData) {
		this.extra = extraData;
	}
	
	public static  HttpResult fromMessageCode(MessageCode mc) {
		HttpResult result = new HttpResult();
		result.setStatus(mc.isSuccess());
        result.setCode(String.valueOf(mc.getCode()));
        result.setMessage(mc.getMessage());
        return result;
	}
	
	public static  HttpResult fromMessageCode(MessageCode mc, T entry) {
		HttpResult result = new HttpResult();
		result.setStatus(mc.isSuccess());
        result.setCode(String.valueOf(mc.getCode()));
        result.setMessage(mc.getMessage());
        result.setEntry(entry);
        return result;
	}

	public static  HttpResult failure() {
        HttpResult result = new HttpResult();
        result.setStatus(false);
        result.setCode(String.valueOf(RESPONSE_FAILURE));
        result.setMessage("");
        return result;
    }
    
    public static  HttpResult failure(String msg) {
        HttpResult result = new HttpResult();
        result.setStatus(false);
        result.setMessage(msg);
        result.setCode(String.valueOf(RESPONSE_FAILURE));
        return result;
    }

    public static  HttpResult failure(int code, String msg) {
        HttpResult result = new HttpResult();
        result.setStatus(false);
        result.setMessage(msg);
        result.setCode(String.valueOf(code));
        return result;
    }
    
    public static  HttpResult success(T obj) {
        SuccessResult result = new SuccessResult();
        result.setStatus(true);
        result.setEntry(obj);
        result.setCode(String.valueOf(RESPONSE_SUCCESS));
        return result;
    }
    
    public static  HttpResult success(T obj, int code) {
        SuccessResult result = new SuccessResult();
        result.setStatus(true);
        result.setCode(String.valueOf(code));
        result.setEntry(obj);
        return result;
    }

    public static  HttpResult> success(List list) {
        SuccessResult> result = new SuccessResult>();
        result.setStatus(true);
        result.setCode(String.valueOf(RESPONSE_SUCCESS));
        if (null == list) {// 若返回数据为null 统一返回给前端[]
            result.setEntry(new ArrayList(0));
        } else {
            result.setEntry(list);
        }
        return result;
    }

    public static HttpResult success() {
        SuccessResult result = new SuccessResult();
        result.setStatus(true);
        result.setEntry(true);
        return result;
    }
    
    public static  HttpResult success(T entry, String message) {
        SuccessResult result = new SuccessResult();
        result.setStatus(true);
        result.setEntry(entry);
        result.setMessage(message);
        return result;
    }
    
    public static  HttpResult> success(PageResultSet obj) {
        PageSuccessResult> result = new PageSuccessResult>();
        result.setStatus(true);
        if (null == obj.getList()) { // 若返回数据为null 统一返回给前端[]
        	    obj.setList(new ArrayList());
        } 
        result.setEntry(obj);

        return result;
    }
    
    public static  HttpResult> page(long pageNo, long pageSize, long total, List list) {
	    PageResultSet pr = new PageResultSet<>(pageNo, pageSize);
	    pr.setTotalRecordNum(total);
	    pr.setList(null == list ? Lists.newArrayList() : list);
	    return success(pr);
    }
    
    /**
     * 带分页信息的结果集数据
     */
    public static class PageSuccessResult extends HttpResult {

		@Override
		public String getMessage() {
			return BizObjects.present(message, HttpResult.MESSAGE_SUCCESS);
		}

		@Override
		public String getCode() {
			return String.valueOf(HttpResult.RESPONSE_SUCCESS);
		}

    }

    public static class SuccessResult extends HttpResult {

		@Override
		public String getMessage() {
			return BizObjects.present(message, HttpResult.MESSAGE_SUCCESS);
		}

		@Override
		public String getCode() {
			return null != code ? code : String.valueOf(HttpResult.RESPONSE_SUCCESS);
		}
        
    }

    public static class FailureResult extends HttpResult {

		@Override
		public String getMessage() {
			return BizObjects.present(message, HttpResult.MESSAGE_FAILURE);
		}

		@Override
		public String getCode() {
			return null != code ? code : String.valueOf(HttpResult.RESPONSE_FAILURE);
		}
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy