Skip to content

Instantly share code, notes, and snippets.

@kdonald
Created March 10, 2012 17:54
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kdonald/2012289 to your computer and use it in GitHub Desktop.
Save kdonald/2012289 to your computer and use it in GitHub Desktop.
RestTemplate + Jackson Solr Client Example
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ObjectNode;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;
@Controller
public class ExampleController {
private ObjectMapper mapper;
private RestTemplate client;
public ExampleController() {
initClient();
}
@RequestMapping(value="/nearby", method=RequestMethod.GET, produces="application/json")
public ResponseEntity<JsonNode> nearby() throws URISyntaxException {
Location location = new Location(28.0674,-80.5595);
// perform a spatial search: all locations within 25 miles of long/lat
JsonNode json = client.getForObject(new URI("http://localhost:8983/solr/select?" +
"wt=json&fl=*&q=*:*&fq=%7B!geofilt%7D&sfield=location&pt=" + location + "&d=25"), JsonNode.class);
JsonNode docs = json.get("response").get("docs");
ObjectNode nearby = mapper.createObjectNode();
nearby.put("nearby", docs);
return new ResponseEntity<JsonNode>(nearby, HttpStatus.ACCEPTED);
}
// internal helpers
private void initClient() {
client = new RestTemplate();
client.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
mapper = new ObjectMapper();
MappingJacksonHttpMessageConverter converter = new MappingJacksonHttpMessageConverter();
converter.setObjectMapper(mapper);
List<MediaType> mediaTypes = new ArrayList<MediaType>(2);
mediaTypes.add(MediaType.APPLICATION_JSON);
// Solr returns text/plain responses for JSON results
mediaTypes.add(MediaType.TEXT_PLAIN);
converter.setSupportedMediaTypes(mediaTypes);
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(1);
converters.add(converter);
client.setMessageConverters(converters);
}
// misc utility code
private final class Location {
private final Double latitude;
private final Double longitude;
public Location(Double latitude, Double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
public Double getLatitude() {
return latitude;
}
public Double getLongitude() {
return longitude;
}
public String toString() {
return getLatitude().toString() + "," + getLongitude().toString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment