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

cn.aradin.spring.swagger.starter.dto.Resp Maven / Gradle / Ivy

There is a newer version: 1.1.2
Show newest version
package cn.aradin.spring.swagger.starter.dto;

import java.io.Serializable;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;

import cn.aradin.spring.core.net.http.code.AradinCodedEnum;
import cn.aradin.spring.core.net.http.code.CodedEnum;
import cn.aradin.spring.core.net.http.error.HttpError;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@SuppressWarnings("serial")
@ApiModel(description = "返回协议结构")
public class Resp implements Serializable{
	@ApiModelProperty(value="操作结果", name="code", required = true)
	private C code;
	@ApiModelProperty(value="错误描述信息", name="msg", required = false)
	private String msg;
	@ApiModelProperty(value="返回数据", name="data", required = false)
	private T data;
	
	public C getCode() {
		return code;
	}
	public void setCode(C 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 Resp data(T data) {
		this.data = data;
		return this;
	}
	public Resp code(C code) {
		this.code = code;
		return this;
	}
	public Resp msg(String msg) {
		this.msg = msg;
		return this;
	}
	
	public boolean ifok() {
		return code.getCode()==0;
	}
	
	public String toString() {
		return JSONObject.toJSONString(this);
	}
	public static Resp ok() {
		return new Resp().code(AradinCodedEnum.OK);
	}
	public static  Resp ok(A data) {
		return new Resp().data(data).code(AradinCodedEnum.OK);
	}
	public static  Resp ok(C coded) {
		Resp resp = new Resp();
		resp.setCode(coded);
		return resp;
	}
	public static  Resp ok(C coded, A data) {
		Resp resp = new Resp();
		resp.setCode(coded);
		resp.setData(data);
		return resp;
	}
	public static Resp error(HttpError error) {
		return error(error, null);
	}
	public static Resp error(HttpError error, String alias) {
		return new Resp()
			.code(error.getCoded())
			.msg(StringUtils.isNotBlank(alias)?alias:error.getMsg());
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy