給自己的requestProxy寫個教程

來源:互聯網
上載者:User

標籤:httpclint

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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://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一樣調用,就沒有對外的感覺了

最後附上自己的地址

git


如果大家有興趣,自己會慢慢寫一些教程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.