liuxiaolong
2019-05-06 c15226e1b58f255dbebf1bdca8d4e53b9277249c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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();
 
    }
}