requestProxy 是自己工作的需要,因为公司的业务,经常要对接第三方也就是各种供应,总是各种tttp,webservice
webservice其实还有如果有的供应商提供了jar,这个注入就来就好了,关键是好多都是http的没有什么可用的(或者我不知道),之前又参与了jeecg的minidao所以
用minidao的思路做了requestProxym就是用来接口和代理来完成所有的请求工作,然后使用Jackson和dom4j来完成对象的解析.
感觉更多多供应返回的xml,自己也写了一个xml解析工具,有需要的可以看下,利用反射和泛型完成常用的XMl解析.
下面介绍下自己的使用方法
1.第一肯定是xml配置,使用了httpclient来做底层的请求,连接池什么的还是大家配置比较好,每个人的需求都不同
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
default-autowire="byName" default-lazy-init="false">
<description>Spring 管理类配置 </description>
<context:annotation-config></context:annotation-config>
<!-- httpclient线程池 -->
<bean id="connectionManagerParams"
class="org.apache.commons.httpclient.params.HttpConnectionManagerParams">
<property name="connectionTimeout" value="120000" />
<property name="soTimeout" value="120000" />
<property name="maxTotalConnections" value="30" />
<property name="defaultMaxConnectionsPerHost" value="20" />
</bean>
<bean id="connectionManager"
class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager">
<property name="params" ref="connectionManagerParams" />
</bean>
<bean id="httpClient" class="org.apache.commons.httpclient.HttpClient">
<constructor-arg>
<ref bean="connectionManager" />
</constructor-arg>
</bean>
<!-- HTTP动态代理类 -->
<bean id="requestHandler" class="com.onepiece.requestproxy.requestproxy.RequestProxyHandler">
<property name="httpClient" ref="httpClient"></property>
</bean>
<!-- 注册 HTTP请求动态代理接口 -->
<bean class="com.onepiece.requestproxy.factory.RequestBeanFactory">
<property name="packagesToScan">
<list>
<value>com.jueyue.onepiece.test.request.*</value>
</list>
</property>
</bean>
</beans>
2.编写接口这里参数和spring 重名了有点不好,大家可以自己重命名下,自己使用地方太多不好改了
@IRequest("testRequest")
public interface ITestRequest {
@IRequestMethod(type = RequestTypeEnum.GET, url = "http://api.map.baidu.com/telematics/v3/weather")
String testGet(@RequestParams("location") String location,
@RequestParams("output") String output,
@RequestParams("ak") String ak);
@IRequestMethod(type = RequestTypeEnum.GET, url = "http://api.map.baidu.com/telematics/v3/weather")
BaiduWeatherEntity testGetEntity(@RequestParams("location") String location,
@RequestParams("output") String output,
@RequestParams("ak") String ak);
}
3.server 里面注入,然后像java一样调用,就没有对外的感觉了
最后附上自己的地址
如果大家有兴趣,自己会慢慢写一些教程