package com.basic.analy.config;
|
|
import com.alibaba.fastjson.JSONObject;
|
import lombok.Data;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
import org.omg.CORBA.portable.ApplicationException;
|
import org.springframework.http.HttpStatus;
|
|
import java.io.Serializable;
|
|
/**
|
* Controller 返回值 公用方法
|
* @author bsk
|
* @date 2018-05-05
|
*
|
*/
|
@Slf4j
|
@Data
|
public class Result implements Serializable {
|
private static final long serialVersionUID = -5398795297842978376L;
|
|
private static final String DEFAULT_OK_MSG = "操作成功!";
|
private static final String DEFAULT_ERROR_APP_MSG = "系统提示:";
|
private static final String DEFAULT_ERROR_SERVER_MSG = "系统服务异常:";
|
|
private String msg;
|
private Integer code;
|
private boolean success;
|
private Object data;
|
|
Result() {
|
}
|
|
|
/**
|
* 操作成功构造
|
* @param msg 返回信息
|
* @param data 返回数据
|
*/
|
Result(String msg, Object data) {
|
this.code = HttpStatus.OK.value();
|
this.msg = StringUtils.isNotBlank(msg) ? msg : DEFAULT_OK_MSG;
|
this.success = true;
|
this.data = data;
|
}
|
|
//
|
|
/**
|
* 操作失败构造
|
* @param e 异常信息
|
*
|
*/
|
Result(Exception e) {
|
if ((e instanceof ApplicationException) || (e instanceof IllegalArgumentException)) {
|
this.code = HttpStatus.FORBIDDEN.value();
|
this.msg = DEFAULT_ERROR_APP_MSG + e.getMessage();
|
this.success = false;
|
log.error(this.msg);
|
} else if(e.getClass().getName().indexOf("FeignException")>0) {
|
this.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
|
this.msg = DEFAULT_ERROR_SERVER_MSG + getFeignExceptionMsg(e);
|
this.success = false;
|
log.error(this.msg);
|
}else{
|
this.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
|
this.msg = DEFAULT_ERROR_SERVER_MSG + e.getMessage();
|
this.success = false;
|
log.error(this.msg);
|
|
}
|
}
|
|
public static Result ok() {
|
return new Result(null, null);
|
}
|
|
public static Result ok(String msg, Object data) {
|
return new Result(msg, data);
|
}
|
|
public static Result error(Exception e) {
|
return new Result(e);
|
}
|
|
public static Result custom(String msg, Integer code, boolean
|
success, Object data) {
|
Result rs = new Result();
|
rs.setCode(code);
|
rs.setData(data);
|
rs.setMsg(msg);
|
rs.setSuccess(success);
|
return rs;
|
}
|
|
//获取feignException信息
|
public static String getFeignExceptionMsg(Exception e){
|
String messge = e.getLocalizedMessage();
|
JSONObject jsStr = JSONObject.parseObject(messge.substring(messge.indexOf("{")));
|
return jsStr.get("message").toString();
|
|
}
|
}
|