discoverer java_java 轻量级 RestClient

packageorg.rx.socks.http;importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONObject;importnet.sf.cglib.proxy.Enhancer;importnet.sf.cglib.proxy.MethodInterceptor;importnet.sf.cglib.proxy.MethodProxy;importorg.apache.commons.lang3.ArrayUtils;importorg.rx.common.Contract;importorg.rx.beans.Tuple;importorg.rx.common.App;importorg.springframework.core.ParameterNameDiscoverer;importorg.springframework.core.PrioritizedParameterNameDiscoverer;importjava.lang.reflect.InvocationHandler;importjava.lang.reflect.Method;importjava.lang.reflect.Parameter;importjava.lang.reflect.Proxy;importjava.util.Arrays;importjava.util.HashMap;importjava.util.Map;importjava.util.function.Function;import staticorg.rx.common.Contract.isNull;public classRestClient {private static class DynamicProxy implementsInvocationHandler, MethodInterceptor {privateString baseUrl, proxyHost;private ParameterNameDiscoverer parameterNameDiscoverer = newPrioritizedParameterNameDiscoverer();privateDynamicProxy(String baseUrl, String proxyHost) {this.baseUrl =baseUrl;this.proxyHost =proxyHost;

}

@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throwsThrowable {if (method.getDeclaringClass().equals(Object.class)) {returnmethod.invoke(proxy, args);

}

String apiPath=method.getName(),

httpMethod= ArrayUtils.isEmpty(args) ?HttpClient.GetMethod : HttpClient.PostMethod;boolean isFormParam = args != null && args.length > 1;

RestMethod restMethod= method.getDeclaredAnnotation(RestMethod.class);if (restMethod != null) {

String temp=isNull(restMethod.path(), restMethod.value());if (!App.isNullOrEmpty(temp)) {

apiPath=temp;

}if (!App.isNullOrEmpty(restMethod.method())) {

httpMethod=restMethod.method();

}

isFormParam=restMethod.isFormParam();

}

String url= String.format("%s/%s", baseUrl, apiPath);

HttpClient client= newHttpClient();

client.setProxyHost(proxyHost);if (App.equals(httpMethod, HttpClient.GetMethod, true)) {returnsetResult(method, client.httpGet(url));

}

Parameter[] parameters=method.getParameters();

String[] parameterNames=parameterNameDiscoverer.getParameterNames(method);

Function func = offset -> !ArrayUtils.isEmpty(parameterNames)&& parameters.length == parameterNames.length ?parameterNames[offset]

: parameters[offset].getName();

System.out.println(method.getDeclaringClass().getName()+ " pNames: " +Arrays.toString(parameterNames));if (!isFormParam && parameters.length == 1) {return setResult(method, client.httpPost(url, args[0]));

}if (!isFormParam) {

JSONObject jsonEntity= newJSONObject();for (int i = 0; i < parameters.length; i++) {

Parameter p=parameters[i];

RestParam restParam= p.getDeclaredAnnotation(RestParam.class);

jsonEntity.put(restParam!= null ?isNull(restParam.name(), restParam.value()) : func.apply(i),

args[i]);

}returnsetResult(method, client.httpPost(url, jsonEntity));

}

Map params = new HashMap<>();for (int i = 0; i < parameters.length; i++) {

Parameter p=parameters[i];

RestParam restParam= p.getDeclaredAnnotation(RestParam.class);

params.put(restParam!= null ?isNull(restParam.name(), restParam.value()) : func.apply(i),

Contract.toJsonString(args[i]));

}returnsetResult(method, client.httpPost(url, params));

}

@Overridepublic Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throwsThrowable {returninvoke(o, method, objects);

}privateObject setResult(Method method, String resText) {

Class> returnType =method.getReturnType();if(returnType.equals(Void.TYPE)) {returnVoid.TYPE;

}

Tuple r =App.tryConvert(resText, returnType);

System.out.println(r.left+ "," + r.right + "=>" + resText + "," +returnType);return r.left ?r.right : JSON.toJavaObject(JSON.parseObject(resText), returnType);

}

}public static T create(Class extends T>restInterface, String baseUrl) {return create(restInterface, baseUrl, null, true);

}public static T create(Class extends T> restInterface, String baseUrl, String proxyHost, booleanbyCglib) {

DynamicProxy handler= newDynamicProxy(baseUrl, proxyHost);return (T) (byCglib ?Enhancer.create(restInterface, handler)

: Proxy.newProxyInstance(handler.getClass().getClassLoader(),newClass[]{restInterface}, handler));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值