Tuesday, December 9, 2008

Using Spring’s HttpInvoker

The spring HTTP invoker is a new remoting model created as part of the Spring Framework to perform remoting across HTTP (to make the firewalls happy) and using Java’s serialization (to make programmers happy).

Client Side
To access services via HTTP, you’ll need to use HttpInvokerProxyFactoryBean.
<bean id="citationService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl">
<value>http://${serverName}/${contextPath}/citation.service</value>
</property>
<property name="serviceInterface">
<value>com.tickettodrive.CitationService</value>
</property>
</bean>



Server Side
You need expose beans as HTTP Services.
<bean id="httpCitationService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service">
<ref bean="citationService"/>
</property>
<property name="serviceInterface">
<value>com.tickettodrive.CitationService</value>
</property>
</bean>

Because HttpInvokerServiceExporter is a Spring MVC controller, you’ll need to set up a URL handler to map an HTTP URL to the service.
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/citation.service">httpCitationService</prop>
</props>
</property>
</bean>

And you’ll also need to deploy the citation service in a web application with Spring’s DispatcherServlet configured in web.xml
<servlet>
<servlet-name>citation</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>citation</servlet-name>
<url-pattern>*.service</url-pattern>
</servlet-mapping>


-- excerpted from Spring In Action 2nd Edition

No comments:

Post a Comment