1.做一个返回码的枚举类
package com.onecity.usualtemplate;
import lombok.Getter;
@Getter
public enum ResponseCode {
SUSECESS(200, "请求成功"),
ERROR(444, "系统未知异常,请联系管理员"),
PARAMETERERROR(600, ""),
AUTHORITYERROR(703, "权限不足");
private Integer key;
private String value;
private ResponseCode(Integer key, String value) {
this.key = key;
this.value = value;
}
}
2.做一个返回实体对象(按需修改,时间是自己写的方法,不用管它)
package com.onecity.usualtemplate;
import com.onecity.comutils.DateTime;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class RequestResult {
/// <summary>
/// 返回code
/// </summary>
private int code;
/// <summary>
/// 返回消息
/// </summary>
private String message;
/// <summary>
/// 服务器当前时间
/// </summary>
private String servertime;
/// <summary>
/// 返回 结果
/// </summary>
private Object result;
/// <summary>
/// 基本构造
/// </summary>
public RequestResult() {
this.code = 200;
this.servertime = DateTime.NOW().tostring(DateCode.YYYY_MM_DD_HH_MM_SS);
}
/// <summary>
/// 基本构造
/// </summary>
/// <param name="result">返回情况</param>
public RequestResult(Object result) {
if (result == null) {
this.code = 400;
this.servertime = DateTime.NOW().tostring(DateCode.YYYY_MM_DD_HH_MM_SS);
return;
}
this.code = 200;
this.message = "操作成功";
this.result = result;
this.servertime = DateTime.NOW().tostring(DateCode.YYYY_MM_DD_HH_MM_SS);
}
/// <summary>
/// 基本构造
/// </summary>
/// <param name="code">状态码</param>
/// <param name="message">消息文本</param>
public RequestResult(ResponseCode code, String message) {
this.code = code.getKey();
this.message = message;
this.servertime = DateTime.NOW().tostring(DateCode.YYYY_MM_DD_HH_MM_SS);
}
public RequestResult(ResponseCode code) {
this.code = code.getKey();
this.message = code.getValue();
this.servertime = DateTime.NOW().tostring(DateCode.YYYY_MM_DD_HH_MM_SS);
}
}
3. 使用,可以选择性覆盖返回提示信息,按需选择
@GetMapping("/querydevicedetail")
public RequestResult QueryDeviceDetail(DeviceDTO device) {
if (StringUtils.isEmpty(device.getId()) && StringUtils.isEmpty(device.getDeviceCode())) {
return new RequestResult(ResponseCode.PARAMETERERROR, "设备不能为空");
}
RequestResult result = deviceservice.QueryDeviceDetail(device);
return result;
}