Hosting Spring MVC controller and Spring-WS service in the same web application -
This is fundamentally useful when developing an application which has its own view and additionally needs to host a bunch of services to its clients. So how do we do this ?
1. web.xml of the web app needs to have 2 set of dispatcher servlets configured
<servlet>
<servlet-name>mvcControllerServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>soapServiceServlet</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Map these servlets to different URL's
<servlet-mapping>
<servlet-name>soapServiceServlet</servlet-name>
<url-pattern>/appname/service/update</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>soapServiceServlet</servlet-name>
<url-pattern>*.wsdl</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>mvcControllerServlet</servlet-name>
<url-pattern>/app-name/mvc/*</url-pattern>
</servlet-mapping>
2. Define 2 spring context XML's mvcControllerServlet-servlet.xml and soapServiceServlet-servlet.xml
mvcControllerServlet-servlet.xml - Define all the mvc controllers in this one
<!-- URL mapping definitions -->
<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="**/info">InfoController</prop>
</props>
</property>
</bean>
<bean id="InfoController" class="com.test.web.InfoController" >
<property name="successView">
<value>mvc/success</value>
</property>
</bean>
soapServiceServlet-servlet.xml - Define messageReceiver etc in this one to facilitate spring SOAP message handling
<bean id="messageReceiver" class="org.springframework.ws.soap.server.SoapMessageDispatcher"/>
<bean id="payloadMapping" class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
<property name="endpointMap">
<map>
<entry key="{http://www.testapp.com/service}UpdateRequest" value-ref="endpoint" />
</map>
</property>
</bean>
Service implementation goes into the endpoint class and mvc implementation can be written in InfoController.
Simple and neat.
This is fundamentally useful when developing an application which has its own view and additionally needs to host a bunch of services to its clients. So how do we do this ?
1. web.xml of the web app needs to have 2 set of dispatcher servlets configured
<servlet>
<servlet-name>mvcControllerServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>soapServiceServlet</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Map these servlets to different URL's
<servlet-mapping>
<servlet-name>soapServiceServlet</servlet-name>
<url-pattern>/appname/service/update</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>soapServiceServlet</servlet-name>
<url-pattern>*.wsdl</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>mvcControllerServlet</servlet-name>
<url-pattern>/app-name/mvc/*</url-pattern>
</servlet-mapping>
2. Define 2 spring context XML's mvcControllerServlet-servlet.xml and soapServiceServlet-servlet.xml
mvcControllerServlet-servlet.xml - Define all the mvc controllers in this one
<!-- URL mapping definitions -->
<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="**/info">InfoController</prop>
</props>
</property>
</bean>
<bean id="InfoController" class="com.test.web.InfoController" >
<property name="successView">
<value>mvc/success</value>
</property>
</bean>
soapServiceServlet-servlet.xml - Define messageReceiver etc in this one to facilitate spring SOAP message handling
<bean id="messageReceiver" class="org.springframework.ws.soap.server.SoapMessageDispatcher"/>
<bean id="payloadMapping" class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
<property name="endpointMap">
<map>
<entry key="{http://www.testapp.com/service}UpdateRequest" value-ref="endpoint" />
</map>
</property>
</bean>
Service implementation goes into the endpoint class and mvc implementation can be written in InfoController.
Simple and neat.
No comments:
Post a Comment