Files
powerjob-kingbase/powerjob-common/src/main/java/tech/powerjob/common/response/AskResponse.java
2025-09-19 16:14:08 +08:00

62 lines
1.6 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
}
}