8000 用XStream代替FastJson · JavaCourse00/JavaCourseCodes@1fa8b77 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1fa8b77

Browse files
committed
用XStream代替FastJson
1 parent 393dd9e commit 1fa8b77

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

07rpc/rpc01/rpcfx-core/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@
2424
<version>1.2.70</version>
2525
</dependency>
2626

27+
<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
28+
<dependency>
29+
<groupId>com.thoughtworks.xstream</groupId>
30+
<artifactId>xstream</artifactId>
31+
<version>1.4.14</version>
32+
</dependency>
33+
34+
<!-- https://mvnrepository.com/artifact/org.codehaus.jettison/jettison -->
35+
<dependency>
36+
<groupId>org.codehaus.jettison</groupId>
37+
<artifactId>jettison</artifactId>
38+
<version>1.4.0</version>
39+
</dependency>
40+
41+
2742
<dependency>
2843
<groupId>com.squareup.okhttp3</groupId>
2944
<artifactId>okhttp</artifactId>

07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/Rpcfx.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
import com.alibaba.fastjson.JSON;
55
import com.alibaba.fastjson.parser.ParserConfig;
6+
import com.thoughtworks.xstream.XStream;
67
import io.kimmking.rpcfx.exception.RpcfxException;
78
import io.kimmking.rpcfx.param.RpcfxRequest;
89
import io.kimmking.rpcfx.param.RpcfxResponse;
10+
import io.kimmking.rpcfx.utils.XStreamUtils;
911
import okhttp3.MediaType;
1012
import okhttp3.OkHttpClient;
1113
import okhttp3.Request;
@@ -19,7 +21,9 @@
1921
public final class Rpcfx {
2022

2123
static {
22-
ParserConfig.getGlobalInstance().addAccept("io.kimmking");
24+
ParserConfig parserConfig = ParserConfig.getGlobalInstance();
25+
parserConfig.addAccept("io.kimmking");
26+
parserConfig.setAutoTypeSupport(true);
2327
}
2428

2529
public static <T> T create(final Class<T> serviceClass, final String url) {
@@ -35,6 +39,7 @@ public static class RpcfxInvocationHandler implements InvocationHandler {
3539

3640
private final Class<?> serviceClass;
3741
private final String url;
42+
private final XStream stream = XStreamUtils.createToJson();
3843

3944
public <T> RpcfxInvocationHandler(Class<T> serviceClass, String url) {
4045
this.serviceClass = serviceClass;
@@ -47,6 +52,7 @@ public <T> RpcfxInvocationHandler(Class<T> serviceClass, String url) {
4752

4853
@Override
4954
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
55+
5056
RpcfxRequest request = new RpcfxRequest();
5157
request.setServiceClass(this.serviceClass.getName());
5258
request.setMethod(method.getName());
@@ -58,7 +64,7 @@ public Object invoke(Object proxy, Method method, Object[] params) throws Throwa
5864
if (!response.isStatus()) {
5965
throw new RpcfxException("invoke error", response.getException());
6066
}
61-
return JSON.parse(response.getResult().toString());
67+
return XStreamUtils.fromBean(stream, response.getResult().toString());
6268
}
6369

6470
private RpcfxResponse post(RpcfxRequest req, String url) throws IOException {

07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/server/RpcfxInvoker.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
import com.alibaba.fastjson.JSON;
44
import com.alibaba.fastjson.serializer.SerializerFeature;
5+
import com.thoughtworks.xstream.XStream;
6+
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
57
import io.kimmking.rpcfx.api.RpcfxReflectionResolver;
68
import io.kimmking.rpcfx.exception.RpcfxException;
79
import io.kimmking.rpcfx.param.RpcfxRequest;
810
import io.kimmking.rpcfx.api.RpcfxResolver;
911
import io.kimmking.rpcfx.param.RpcfxResponse;
12+
import io.kimmking.rpcfx.utils.XStreamUtils;
1013

1114
import java.lang.reflect.InvocationTargetException;
1215
import java.lang.reflect.Method;
@@ -37,7 +40,9 @@ public RpcfxResponse invoke(RpcfxRequest request) {
3740
Method method = resolveMethodFromClass(service.getClass(), request.getMethod());
3841
Object result = method.invoke(service, request.getParams()); // dubbo, fastjson,
3942
// 两次json序列化能否合并成一个
40-
response.setResult(JSON.toJSONString(result, SerializerFeature.WriteClassName));
43+
//response.setResult(JSON.toJSONString(result, SerializerFeature.WriteClassName));
44+
XStream stream = XStreamUtils.createToJson();
45+
response.setResult(XStreamUtils.to(stream, result));
4146
response.setStatus(true);
4247
return response;
4348
} catch ( IllegalAccessException | InvocationTargetException e) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.kimmking.rpcfx.utils;
2+
3+
import com.thoughtworks.xstream.XStream;
4+
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
5+
6+
public class XStreamUtils {
7+
8+
private static XStream stream;
9+
10+
public static XStream createToJson() {
11+
stream = new XStream(new JettisonMappedXmlDriver());
12+
stream.setMode(XStream.NO_REFERENCES);
13+
return stream;
14+
}
15+
16+
public static <T> T fromBean(XStream stream, String str) {
17+
if (null != stream) {
18+
return (T) stream.fromXML(str);
19+
}
20+
return null;
21+
}
22+
23+
public static String to(XStream stream, Object obj) {
24+
if (null != stream) {
25+
return stream.toXML(obj);
26+
}
27+
return null;
28+
}
29+
30+
31+
public static XStream createToXml() {
32+
stream = new XStream();
33+
return stream;
34+
}
35+
36+
}

0 commit comments

Comments
 (0)
0