后台proxy代理通过Node.js获取数据返回生成Echarts(proxy)

后台proxy代理

首先是搭建proxy

  • JDK和cglib都是可以用来做代理,所以学习了解了一下
  • JDK只能针对实现了接口的类生成代理
  • cglib是针对类实现代理,可以实现也可以不实现

Maven

<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>3.2.5</version>
</dependency>

NodeService.java

public interface NodeService {
  public String getJson(String url);
}

NodeServiceImpl.java

public class NodeServiceImpl implements NodeService {

  @Override
  public String getJson(String url) {
    String result = HttpClientUtil.getWebCon(url).replaceAll("&quot;", "\"");
    return result;
  }

}

NodeProxy.java

public class NodeProxy implements MethodInterceptor {

  @Override
  public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy)
      throws Throwable {
    Object o1 = methodProxy.invokeSuper(o, args);
    return o1;
  }

}

test.java

      NodeProxy nodeProxy = new NodeProxy();
      Enhancer enhancer = new Enhancer();
      enhancer.setSuperclass(NodeServiceImpl.class);
      enhancer.setCallback(nodeProxy);
      NodeService o = (NodeService) enhancer.create();
      json = o.getJson(url);

你可能感兴趣的:(mysql,node.js)