1- package io .kimmking .rpcfx .client ;
2-
1+ package io .kimmking .rpcfx .proxy ;
32
43import com .alibaba .fastjson .JSON ;
54import com .alibaba .fastjson .parser .ParserConfig ;
98import okhttp3 .OkHttpClient ;
109import okhttp3 .Request ;
1110import okhttp3 .RequestBody ;
11+ import org .aopalliance .intercept .MethodInterceptor ;
1212
1313import java .io .IOException ;
1414import java .lang .reflect .InvocationHandler ;
1515import java .lang .reflect .Method ;
16- import java .lang .reflect .Proxy ;
17-
18- public final class Rpcfx {
19-
20- static {
21- ParserConfig .getGlobalInstance ().addAccept ("io.kimmking" );
22- }
2316
24- public static <T > T create (final Class <T > serviceClass , final String url ) {
25-
26- // 0. 替换动态代理 -> AOP
27- return (T ) Proxy .newProxyInstance (Rpcfx .class .getClassLoader (), new Class []{serviceClass }, new RpcfxInvocationHandler (serviceClass , url ));
28-
29- }
30-
31- public static class RpcfxInvocationHandler implements InvocationHandler {
17+ public class RpcfxInvocationHandler implements InvocationHandler {
3218
3319 public static final MediaType JSONTYPE = MediaType .get ("application/json; charset=utf-8" );
3420
@@ -37,6 +23,7 @@ public static class RpcfxInvocationHandler implements InvocationHandler {
3723 public <T > RpcfxInvocationHandler (Class <T > serviceClass , String url ) {
3824 this .serviceClass = serviceClass ;
3925 this .url = url ;
26+ ParserConfig .getGlobalInstance ().setAutoTypeSupport (true );
4027 }
4128
4229 // 可以尝试,自己去写对象序列化,二进制还是文本的,,,rpcfx是xml自定义序列化、反序列化,json: code.google.com/p/rpcfx
@@ -45,20 +32,25 @@ public <T> RpcfxInvocationHandler(Class<T> serviceClass, String url) {
4532
4633 @ Override
4734 public Object invoke (Object proxy , Method method , Object [] params ) throws Throwable {
48- RpcfxRequest request = new RpcfxRequest ();
49- request .setServiceClass (this .serviceClass .getName ());
50- request .setMethod (method .getName ());
51- request .setParams (params );
35+ return invokePost (method , params );
36+ }
5237
53- RpcfxResponse response = post (request , url );
38+ private Object invokePost (Method method , Object [] params ) throws IOException {
39+ RpcfxRequest request = new RpcfxRequest ();
40+ request .setServiceClass (this .serviceClass .getName ());
41+ request .setMethod (method .getName ());
42+ request .setParams (params );
43+ request .setParameterTypes (method .getParameterTypes ());
5444
55- // 这里判断response.status,处理异常
56- // 考虑封装一个全局的RpcfxException
45+ RpcfxResponse response = post (request , url );
5746
58- return JSON . parse ( response . getResult (). toString ());
59- }
47+ // 这里判断response.status,处理异常
48+ // 考虑封装一个全局的RpcfxException
6049
61- private RpcfxResponse post (RpcfxRequest req , String url ) throws IOException {
50+ return JSON .parse (response .getResult ().toString ());
51+ }
52+
53+ private RpcfxResponse post (RpcfxRequest req , String url ) throws IOException {
6254 String reqJson = JSON .toJSONString (req );
6355 System .out .println ("req json: " +reqJson );
6456
@@ -73,5 +65,4 @@ private RpcfxResponse post(RpcfxRequest req, String url) throws IOException {
7365 System .out .println ("resp json: " +respJson );
7466 return JSON .parseObject (respJson , RpcfxResponse .class );
7567 }
76- }
77- }
68+ }
0 commit comments