While giving a series of presentations on using Java in a distributed environment (focusing on Java EE and Spring), I got a lot of interest in web programming. I did an extra presentation on servlets, but I didn’t want to leave it there, because writing Java servlets directly is not very efficient compared to tools like Spring WebMVC.
So I made an extra presentation on WebMVC, which led me to make a sample application that provides both a client and a server using Spring WebMVC. I’ll describe the client and the server in subsequent posts, but first I want to talk about one little extra package I included for the sake of the class.
Homemade examples with silly capabilities can be pretty boring. I still think it’s important to teach that way most of the time, because it avoids getting students hung up in issues with the business logic when what you’re trying to present is how the integration takes place. So my example client and server provide the ability to submit ‘add’, ‘subtract’, ‘multiply’, and ‘divide’ jobs to a server, which returns the result.
I figured that would bore everyone to the extent that the power of Spring WebMVC wouldn’t come through. So I made an extra little client that queries Google’s Places API for local food shops. The key thing was to show a slightly more real-world example, and to also show how easy WebMVC makes these things.
Even though I’m using Spring WebMVC for the client, I decided not to use a Spring application context for this example, because it’s too simple for that. So the Java code just looks like this:
The heavy lifting is done by Spring’s RestTemplate. It handles crafting the HTTP GET, encoding the URL using the template provided, setting HTTP headers, and doing whatever conversions are necessary for both request and response.
What comes back from Google’s servers looks in part like this:
You can see in the Java code that I’ve explicitly provided the template with a
message converter using Jackson. In a future post I’ll talk about the
registration happening automatically as part of Spring’s annotation-driven
detection of REST endpoints. The JSON response that comes back from Google’s
servers is converted by Jackson into a
PlaceResponse instance. The
PlaceResponse class is below; its helper classes are shown in the webapp.
What’s nice is that the classes are just POJOs, though the names are a little
odd to match the JSON as this avoids some Jackson annotations.
There’s more in the JSON than I bothered to put into the Java classes, but
the Jackson annotation
@JsonIgnoreProperties(ignoreUnknown = true) on the
class tells Jackson not to get worked up about that.
Because this is part of a larger sample app, I didn’t go to the trouble of adding
pom.xml so it could be run from the command line. Also, in order to get
it to work, it’s necessary to pass it a command-line parameter with your own
Google API key.
Some acknowledgements: my original version used Ant with no dependency management, so I’m appreciative for an example of WebMVC with Maven. For some reason the computer on which I ran the examples did not like the SSL certs from Google, so this page was a big help too.