xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="restTemplate"
class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="castorMarshaller"/>
<property name="unmarshaller" ref="castorMarshaller"/>
<property name="supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="application"/>
<constructor-arg index="1" value="xml"/>
</bean>
</list>
</property>
</bean>
</list>
</property>
</bean>
<bean id="castorMarshaller"
class="org.springframework.oxm.castor.CastorMarshaller">
<property name="mappingLocation" value="classpath:oxm-mapping.xml"/>
</bean>
</beans>
As shown in Listing 16-29, a restTemplate bean is declared using the RestTemplate class. With the
class, the property messageConverters is injected with an instance of MarshallingHttpMessageConverter
using Castor, the same as the one on the server side. The mapping file will be shared among both the
server and client sides. In addition, for the restTemplate bean, within the anonymous class
MarshallingHttpMessageConverter, the property supportedMediaTypes is injected with an anonymous
bean declaration of an MediaType instance, indicating that the only supported media is XML. As a result,
the client is always expecting XML as the return data format, and Castor will help perform the
conversion between POJO and XML.
Let's try the service to get all contacts first. Listing 16-30 shows the main testing program.
Listing 16-30. Testing RestTemplate
package com.apress.prospring3.ch16.restful;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.web.client.RestTemplate;
import com.apress.prospring3.ch16.domain.Contact;
import com.apress.prospring3.ch16.domain.Contacts;
public class RestfulClientSample {
private static final String URL_GET_ALL_CONTACTS =
"http://localhost:8080/ch16/restful/contact/listdata";
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home