2. HttpClientOxmTemplate

HttpClientOxmTemplate which uses Apache's HttpClient to process HTTP requests and Spring Web Service's OXM to marshall and unmarshall the requests.

Spring Configuration

Define a marshaller/unmarshaller and set it on HttpClientOxmTemplate along with it's default URI.

HttpClientOxmTemplateTest-context.xml
                    
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="contextPath" value="org.springbyexample.schema.beans"/>
    </bean>
    
    <bean id="httpClient" class="org.springbyexample.httpclient.HttpClientOxmTemplate">
        <property name="defaultUri">
            <value>http://localhost:8093/test</value>
        </property>
        <property name="marshaller" ref="jaxbMarshaller" />
        <property name="unmarshaller" ref="jaxbMarshaller" />
    </bean>

</beans>
                    
                

Code Example

Example 9. Excerpt from HttpClientOxmTemplateTest.testStringDataPostMethodWithStringResponse()

The instance of Persons is marshalled and the XML is posted to the default uri and the callback has the XML result unmarshalled back to an instance of Persons. The class passed in and returned in this example match, but they don't need to since a different marshaller and unmarshaller can be used.

                    
template.executePostMethod(persons, new ResponseCallback<Persons>() {
    public void doWithResponse(Persons persons) throws IOException {
        ...
        
        Person result = persons.getPerson().get(0);
        
        ...

        logger.debug("id={}  firstName={}  lastName={}", 
                     new Object[] { result.getId(), 
                                    result.getFirstName(), 
                                    result.getLastName()});
    }
});