|
@@ -30,14 +30,25 @@ public class XxlJobNetCommUtil {
|
30
|
30
|
// hex param key
|
31
|
31
|
public static final String HEX = "hex";
|
32
|
32
|
|
|
33
|
+
|
33
|
34
|
/**
|
34
|
35
|
* format object to hex-json
|
35
|
36
|
* @param obj
|
36
|
37
|
* @return
|
37
|
38
|
*/
|
38
|
39
|
public static String formatObj2HexJson(Object obj){
|
|
40
|
+ // obj to json
|
39
|
41
|
String json = JacksonUtil.writeValueAsString(obj);
|
40
|
|
- String hex = ByteHexConverter.byte2hex(json.getBytes());
|
|
42
|
+ int len = ByteHexConverter.getByteLen(json);
|
|
43
|
+
|
|
44
|
+ // json to byte[]
|
|
45
|
+ ByteWriteFactory byteWriteFactory = new ByteWriteFactory();
|
|
46
|
+ byteWriteFactory.writeInt(len);
|
|
47
|
+ byteWriteFactory.writeString(json, len);
|
|
48
|
+ byte[] bytes = byteWriteFactory.getBytes();
|
|
49
|
+
|
|
50
|
+ // byte to hex
|
|
51
|
+ String hex = ByteHexConverter.byte2hex(bytes);
|
41
|
52
|
return hex;
|
42
|
53
|
}
|
43
|
54
|
|
|
@@ -48,14 +59,25 @@ public class XxlJobNetCommUtil {
|
48
|
59
|
* @return
|
49
|
60
|
*/
|
50
|
61
|
public static <T> T parseHexJson2Obj(String hex, Class<T> clazz){
|
51
|
|
- String json = new String(ByteHexConverter.hex2Byte(hex));
|
|
62
|
+ // hex to byte[]
|
|
63
|
+ byte[] bytes = ByteHexConverter.hex2Byte(hex);
|
|
64
|
+
|
|
65
|
+ // byte[] to json
|
|
66
|
+ ByteReadFactory byteReadFactory = new ByteReadFactory(bytes);
|
|
67
|
+ String json = byteReadFactory.readString(byteReadFactory.readInt());
|
|
68
|
+
|
|
69
|
+ // json to obj
|
52
|
70
|
T obj = JacksonUtil.readValue(json, clazz);
|
53
|
71
|
return obj;
|
54
|
72
|
}
|
55
|
73
|
|
56
|
74
|
public static void main(String[] args) {
|
57
|
|
- System.out.println(parseHexJson2Obj("7B2274696D657374616D70223A313436393432323136303032362C22616374696F6E223A2252554E222C226A6F6247726F7570223A2264656661756C7473222C226A6F624E616D65223A22323031363037323530393030353730363632222C226578656375746F7248616E646C6572223A2264656D6F4A6F6248616E646C6572222C226578656375746F72506172616D73223A2231303030303030222C22676C7565537769746368223A66616C73652C226C6F6741646472657373223A2231302E35372E3132332E32383A38383838222C226C6F674964223A3138382C226C6F674461746554696D223A302C22737461747573223A2253554343455353222C226D7367223A6E756C6C7D", RequestModel.class));
|
58
|
|
- System.out.println(parseHexJson2Obj("7B22737461747573223A2253554343455353222C226D7367223A6E756C6C7D", ResponseModel.class));
|
|
75
|
+ RequestModel requestModel = new RequestModel();
|
|
76
|
+ requestModel.setJobGroup("group");
|
|
77
|
+
|
|
78
|
+ String hex = formatObj2HexJson(requestModel);
|
|
79
|
+ System.out.println(hex);
|
|
80
|
+ System.out.println(parseHexJson2Obj(hex, RequestModel.class));
|
59
|
81
|
}
|
60
|
82
|
|
61
|
83
|
/**
|
|
@@ -94,8 +116,12 @@ public class XxlJobNetCommUtil {
|
94
|
116
|
EntityUtils.consume(entity);
|
95
|
117
|
|
96
|
118
|
// i do not know why
|
97
|
|
- responseHex = responseHex.replace("\n", "");
|
98
|
|
- responseHex = responseHex.replace("\r", "");
|
|
119
|
+ //responseHex = responseHex.replace("\n", "");
|
|
120
|
+ //responseHex = responseHex.replace("\r", "");
|
|
121
|
+
|
|
122
|
+ if (responseHex!=null) {
|
|
123
|
+ responseHex = responseHex.trim();
|
|
124
|
+ }
|
99
|
125
|
|
100
|
126
|
// parse hex-json to ResponseModel
|
101
|
127
|
ResponseModel responseModel = XxlJobNetCommUtil.parseHexJson2Obj(responseHex, ResponseModel.class);
|