Write a tutorial for your requestproxy

Source: Internet
Author: User

Requestproxy is the need for your own work, because the company's business, often need to connect to a third party, that is, a variety of supply, always a variety of tttp, WebService

In fact, if some vendors provide jar for WebService, this injection will be fine. The key is that many of them are HTTP and there is no available (or I don't know ), I have participated in the minidao of jeecg before.

Using the minidao idea, requestproxym is used to interface and proxy to complete all request work, and then Jackson and dom4j are used to complete object parsing.

I feel more about the XML returned. I have also written an XML parsing tool. If you need it, you can check it and use reflection and generics to complete common XML parsing.

The following describes how to use it.

1. First, it must be xml configuration. httpclient is used for the underlying request. The connection pool or something is better configured for everyone, and everyone's requirements are different.

<? 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: Schema Location = "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.springframew Butler 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 Management Configuration </description> <context: annotation-config> </Context: annotation-config> <! -- Httpclient thread pool --> <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. httpclie NT. 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 dynamic proxy class --> <bean id = "requesthandler" class = "com. onepiece. requestproxy. requestproxy. requestproxyhandler "> <property name =" httpclient "ref =" httpclient "> </property> </bean> <! -- Register an HTTP request dynamic proxy interface --> <Bean class = "com. onepiece. requestproxy. factory. requestbeanfactory "> <property name =" packagestoscan "> <list> <value> COM. jueyue. onepiece. test. request. * </value> </List> </property> </bean> </beans>

2. It is a bit difficult to rename parameters and spring in writing interfaces. You can rename the parameters and change the parameters if they are used too much.

@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. Inject in the server and call it like Java, so there is no external feeling.

Add your own address.

Git


If you are interested, you will write some tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.