Web 2.0, REST and BEA Workshop
Bill Roth's Blog |
February 4, 2007 8:49 PM
|
Comments (4)
There has been a lot of talk lately about Web 2.0. My impressions is that if yo you ask 10 people what Web 2.0 is, you will get 11 answers. To some, its social networking. To developers, it's most likely something to do with REST, or Representational State Transfer, an idea that came up in Roy Fielding's thesis. To that end, someone asked me recently, "How come you don't do REST?" My answer is that we have been doing this for quite a while, but I would like to prove it. This post will talk about my recent attempt to build a REST client to a REST service in Workshop. Some have regarded Web Services in general, and SOAP messaging specifically as too heavyweight. An number of web sites and web companies have started to include REST APIs to their sites and their underlying functions. In order to see how difficult it would be, I decided to embark on building an example which shows how it could be done. The service I picked was the Yahoo GeoCode service. This is a yahoo service which, when given a physical address, returns the longitude and latitude. You can find the description of the service on the Yahoo site. Yahoo actually does a great job of making it easy to use their services. The full source code can be found on CodeShare. The request The model for REST APIs is for an HTTP URL to be constructed, and XML document returned. In this example, to make the initial query you need to append the strings taken from the input form: String queryURL,q1,yahooResults; queryURL = YAHOO_GEOCODE_URL + "appid=" + YAHOO_APPL_ID + "&" + "street=" + street + "&" + "city=" + city + "&" + "state=" + state + "&" + "zip=" + zip + "&" + "location=" + location + "&" + "output=xml" q1 = replaceSpacePlus(queryURL);//make sure spaces are replaced with plusses //Now call the web service and get the results. yahooResults = getYahooGeocodeResults(q1);
In order to make request, all that is needed is to make a simple HTTP request(I got help with this from David Read): StringBuffer buffer = new StringBuffer(); try { //try to set URL URL url = new URL(queryUrl); // Create a connection HttpURLConnection connection = new HttpURLConnection(url); connection.setRequestMethod("GET"); // now connect connection.connect(); // get an input stream InputStream is = connection.getInputStream(); //create the reader BufferedReader in = new BufferedReader(new InputStreamReader(is)); String line; //while still reading in while ((line = in.readLine()) != null) { //add line to string buffer buffer.append(line); } Once the request is made, the results are put into a String and return to be handled. The response Now that we have the XML response in the string, we need to parse it. In order to make this easy, XML Beans will be used. Yahoo provides a XML Schema to help with this. So, take the XML Schema file, and add to the schemas files. This will generate the definitions you need to parse the file. The response is in this format:
<xs:complexType name="ResultType"> <xs:sequence> <xs:element name="Latitude" type="xs:decimal" /> <xs:element name="Longitude" type="xs:decimal" /> <xs:element name="Address" type="xs:string" /> <xs:element name="City" type="xs:string" /> <xs:element name="State" type="xs:string" /> <xs:element name="Zip" type="xs:string" /> <xs:element name="Country" type="xs:string" /> </xs:sequence> <xs:attribute name="precision" type="xs:string" /> <xs:attribute name="warning" type="xs:string" use="optional"/> </xs:complexType> Now that we have set up the XML infrastructure, we're ready to parse the results into something meaningful. try { rsd = ResultSetDocument.Factory.parse(yahooResults); // Parse the resultant string } catch (XmlException e) { // catch any parse errors e.printStackTrace(); return null; } ResultSetDocument.ResultSet rs = rsd.getResultSet(); // Pull out the result set ResultType rt = rs.getResultArray(0); // get the answer array, and return. Note that ResultSetDocument, ResultSet and ResultType come from the JAR files generated by XML beans. Once you have a handle on the result info, you can then pass it as an action output from page flow, and then show the data by using JSP expression language statements like: ${pageInput.getYahooGeocodeResult.longitude}. For more info, see the code sample I submitted to code share.
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
I sort of see why you latch onto REST, but I think it's important to not conflate REST with Web 2.0. Though many folk use REST in "Web 2.0" apps - it's not a requirement. I think there is a very good definition of web 2.0 - what is web 2.0. Then again, I'm biased ;-)
Posted by: jonmountjoy on February 5, 2007 at 1:03 AM
-
Good point. Thanks for the link.
Posted by: wgroth2 on February 5, 2007 at 3:09 PM
-
Great link re web2.0. It was an insightful read especially when you consider the impact aspects such as blogging has had. The problem now with such open and immediate access to uncensored information is what to believe and what not to believe. I guess web3.0 will focus on controlling the sprawl all of us hooked on the web2.0 candy put out there for others to consume without ever reading the label and verifying the sources :)
Posted by: quinton_wall on February 22, 2007 at 3:54 PM
-
We've been looking into making data and content available via REST for use in Web 2.0 applications, and how to do so in a way that helps to manage the sprawl that Quinton mentions. My recent blogs have covered the prototype REST commands on wlp.bea.com, including how to use them with various RIA technologies such as Adobe Flex:
REST-Powered Flex Applications to Access wlp.bea.com.
I'm very interested in how we can make "Web 2.0" relevant, especially to enterprise customers. The RIA technologies are great, but they won't be useful if the users can't get to their data, content, services, and applications.
Posted by: skip on July 16, 2007 at 3:34 PM
|