Writing a Spring web client to get JSON response, demo with yahoo finance API client

Using spring framework to write a RESTful Http client is simple and takes only a few lines of code. A lot of the connection establishment and request creation details are done by the RestTemplate class. Below is a demonstration of a web client issuing a RESTful query to yahoo.finance.quotes to get quotes for a given symbol. The below code uses Spring's RestTemplate class to create a request and retrieve response from yahoo's YQL(Yahoo Query Language) web server. I hope some of you reading this post will find it informative.

@Autowired
RestTemplate rTemplate;
@RequestMapping(value=”/getquote”, method=RequestMethod.GET)
public @ResponseBody Wrapper retrieve(String requestedSymbol) {
  String env = “store://datatables.org/alltableswithkeys”;
  String symbolString = “(” + requestedSymbol + ”)";
  String fmt="json";
  String queryStr = “SELECT from yahoo.finance.quotes where symbol in “;
  String restJsonUrl = "http://query.yahooapis.com/v1/public/yql?q={qid}{symbol}&format={fmt}&env={senv}";
  Wrapper response =rTemplate.getForObject(restJsonUrl, Wrapper, queryStr,symbolString,fmt,env);
  return response;
}

Below is an extract of the JSON format result returned by running the above piece of code. I have defined the below classes to map this result returned by the YQL server.

{"query":{"results":{"quote":[{"Symbol":"FB","LastTradeDate":"7/5/2012","Open":"31.30","PreviousClose":"31.20","Ask":"31.07"}]}}}

@JsonIgnoreProperties(ignoreUnknown = true)
public class Wrapper {

  @JsonProperty("query")
  public JResponse rs;

  Wrapper() {      }
}
public class JResponse {

@JsonProperty("results")
public JQuoteHelper jquoteHelp;

//setters/getters

}

class JQuoteHelper {

@JsonProperty("quote")
public List<JQuote> quotes;

//more fields, getters/setters declared here

}

@JsonIgnoreProperties(ignoreUnknown = true)
public class JQuote {

  public String Symbol;
  public String LastTradeDate;
  public String Open;
  public String PreviousClose;
  public String Ask;

//more fields declared here

}

Here are the dependencies for the above code to work with Spring framework.
1) Define rTemplate(RestTemplate) in the xml file that defines the applications root application context. It is the xml file at the "contextConfigLocation" as mentioned in your web.xml file.


<bean id="rTemplate" class="org.springframework.web.client.RestTemplate">
  <property name="messageConverters">
  <list>
    <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
  </list>
  </property>
</bean>

2) Declare the following dependency, where ${org.jackson-version} is the version of jackson you use.
<!-- JSON Support -->

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${org.jackson-version}</version>
</dependency>

How to test it:

Startyour webserver http://localhost:port/webappname/getquote

NOTE: Google chrome has a good JSON renderer. If using IE it will prompt download of the JSON response onto your machine.

One Response to Writing a Spring web client to get JSON response, demo with yahoo finance API client

  1. Pingback: Live stock quote listing using GWT and Spring « mytwocentsads

Leave a comment