WangHan
2024-09-12 d5855a4926926698b740bc6c7ba489de47adb68b
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
package tech.powerjob.common.response;
 
import tech.powerjob.common.PowerSerializable;
import tech.powerjob.common.serialize.JsonUtils;
import lombok.*;
 
import java.nio.charset.StandardCharsets;
 
 
/**
 * Pattens.ask 的响应
 *
 * @author tjq
 * @since 2020/3/18
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AskResponse implements PowerSerializable {
 
    private boolean success;
 
    /*
    - 使用 Object 会报错:java.lang.ClassCastException: scala.collection.immutable.HashMap cannot be cast to XXX,只能自己序列化反序列化了
    - 嵌套类型(比如 Map<String, B>),如果B也是个复杂对象,那么反序列化后B的类型为 LinkedHashMap... 处理比较麻烦(转成JSON再转回来)
    - 考虑到多语言通讯,data 必须使用 JSON 序列化为字节数组
     */
    private byte[] data;
 
    // 错误信息
    private String message;
 
    public static AskResponse succeed(Object data) {
        AskResponse r = new AskResponse();
        r.success = true;
        if (data != null) {
            if (data instanceof String) {
                r.data = ((String) data).getBytes(StandardCharsets.UTF_8);
            } else {
                r.data = JsonUtils.toBytes(data);
            }
        }
        return r;
    }
 
    public static AskResponse failed(String msg) {
        AskResponse r = new AskResponse();
        r.success = false;
        r.message = msg;
        return r;
    }
 
    public <T> T getData(Class<T> clz) throws Exception {
        return JsonUtils.parseObject(data, clz);
    }
 
    public String parseDataAsString() {
        return new String(data, StandardCharsets.UTF_8);
    }
 
}