An HttpClientTemplate
and HttpClientOxmTemplate
are
configured. The former is used for non-marshalling requests and
the latter is for marhsallin requests.
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- Loads CatalogItemMarshaller --> <context:component-scan base-package="org.springbyexample.enterprise.solr" /> <context:property-placeholder location="org/springbyexample/enterprise/solr/solr.properties"/> <bean id="selectUrl" class="java.lang.String"> <constructor-arg value="http://${solr.host}:${solr.port}/solr/select" /> </bean> <bean id="updateUrl" class="java.lang.String"> <constructor-arg value="http://${solr.host}:${solr.port}/solr/update" /> </bean> <bean id="selectTemplate" class="org.springbyexample.httpclient.HttpClientTemplate" p:defaultUri-ref="selectUrl" /> <bean id="updateTemplate" class="org.springbyexample.httpclient.HttpClientTemplate" p:defaultUri-ref="updateUrl" /> <bean id="marshallingSelectTemplate" class="org.springbyexample.httpclient.HttpClientOxmTemplate" p:defaultUri-ref="selectUrl" p:marshaller-ref="catalogItemMarshaller" p:unmarshaller-ref="catalogItemMarshaller" /> <bean id="marshallingUpdateTemplate" parent="marshallingSelectTemplate" p:defaultUri-ref="updateUrl" /> </beans>
A basic search, paginated search, and update will be shown using marshalling and unmarshalling with
HttpClientOxmTemplate
. Also HttpClientTemplate
is used to make
search requests and log the results for debugging.
A simple search is made by passing in the search query as the key of 'q'.
The selectTemplate
is the HttpClientTemplate
and
performs a search based Map
and logs the results. The same
Map
is then used by the marshallingSelectTemplate
to make a request that is unmarshalled into a list of CatalogItem
s.
Example 8. Excerpt from SolrHttpClientTemplateIT.testSearch()
public void testSearch() { assertNotNull("HttpClientOxmTemplate is null.", marshallingSelectTemplate); Map<String, String> hParams = new HashMap<String, String>(); hParams.put("q", SEARCH_QUERY_PARAM); hParams.put("indent", "on"); // just for debugging selectTemplate.executeGetMethod(hParams, new ResponseStringCallback() { public void doWithResponse(String response) throws IOException { logger.debug(response); } }); marshallingSelectTemplate.executeGetMethod(hParams, new ResponseCallback<List<CatalogItem>>() { public void doWithResponse(List<CatalogItem> lCatalogItems) throws IOException { assertNotNull("Catalog item list is null.", lCatalogItems); int expectedCount = 2; assertEquals("Catalog item list should be '" + expectedCount + "'.", expectedCount, lCatalogItems.size()); CatalogItem item = lCatalogItems.get(0); logger.debug("id={} manufacturer={} name={} price={} inStock={} popularity={}", new Object[] { item.getId(), item.getManufacturer(), item.getName(), item.getPrice(), item.isInStock(), item.getPopularity() }); ... } }); }
This search is paginated since it defines what range of results to return,
which are defined by the 'start' key and the 'rows' key. The
search parameter is defined by the 'q' key, and searches for any
record that has 'electronics' in it. The 'indent' key is
useful for having formatted results logged by selectTemplate
for debugging.
Example 9. Excerpt from SolrHttpClientTemplateIT.testPaginatedSearch()
public void testPaginatedSearch() { assertNotNull("HttpClientOxmTemplate is null.", marshallingSelectTemplate); Map<String, String> hParams = new HashMap<String, String>(); hParams.put("q", "electronics"); hParams.put("start", "5"); hParams.put("rows", "5"); hParams.put("indent", "on"); // just for debugging selectTemplate.executeGetMethod(hParams, new ResponseStringCallback() { public void doWithResponse(String response) throws IOException { logger.debug(response); } }); marshallingSelectTemplate.executeGetMethod(hParams, new ResponseCallback<List<CatalogItem>>() { public void doWithResponse(List<CatalogItem> lCatalogItems) throws IOException { assertNotNull("Catalog item list is null.", lCatalogItems); int expectedCount = 5; assertEquals("Catalog item list should be '" + expectedCount + "'.", expectedCount, lCatalogItems.size()); CatalogItem item = lCatalogItems.get(0); logger.debug("id={} manufacturer={} name={} price={} inStock={} popularity={}", new Object[] { item.getId(), item.getManufacturer(), item.getName(), item.getPrice(), item.isInStock(), item.getPopularity() }); ... } }); }
Adds or updates a list of JavaBeans and then calls commit. The add or update isn't committed until a commit is sent.
Example 10. Excerpt from SolrHttpClientTemplateIT.testUpdate()
public void testUpdate() { ... List<CatalogItem> lCatalogItems = new ArrayList<CatalogItem>(); CatalogItem item = new CatalogItem(); item.setId(CATALOG_ITEM_ID); item.setManufacturer(CATALOG_ITEM_MANUFACTURER); item.setName(CATALOG_ITEM_NAME); item.setPrice(CATALOG_ITEM_PRICE); item.setInStock(CATALOG_ITEM_IN_STOCK); item.setPopularity(expectedPopularity); lCatalogItems.add(item); marshallingUpdateTemplate.executePostMethod(lCatalogItems); // have to commit the updates updateTemplate.executePostMethod(COMMIT); ... }