public class HsfFactoryBean
private String url;
private Class
private String version;
static {
ParserConfig.getGlobalInstance().addAccept("com.");
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
}
@SuppressWarnings("unchecked")
@Override
public T getObject() throws Exception {
return (T) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{serviceInterface}, this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String dest = this.url + "/" + serviceInterface.getName() + ":" + version + "/" + method.getName();
log.info("invoke " + dest);
Object[] argTypes = Stream.of(method.getParameterTypes()).map(Class::getName).collect(Collectors.toList()).toArray();
List
if (arg == null) {
return null;
}
if (ClassUtils.isPrimitiveOrWrapper(arg.getClass())) {
return arg;
}
if (String.class.isAssignableFrom(arg.getClass())) {
return "\"" + arg + "\"";
}
if (Enum.class.isAssignableFrom(arg.getClass())) {
return "\"" + arg + "\"";
}
return JSONObject.toJSON(arg);
}).collect(Collectors.toList());
DmHttpRequest request = DmHttpRequest.dest(dest)
.form("ArgsTypes", JSONObject.toJSONString(argTypes))
.form("ArgsObjects", JSONObject.toJSONString(argsList))
.send();
try {
return request.resolve(method.getGenericReturnType());
} catch (Exception ex) {
throw new Exception("异常: " + method.getName());
}
}
@Override
public Class> getObjectType() {
return serviceInterface;
}
@Override
public boolean isSingleton() {
return true;
}
public void setUrl(String url) {
this.url = url;
}
public void setServiceInterface(Class
this.serviceInterface = serviceInterface;
}
public void setVersion(String version) {
this.version = version;
}
}