Safeguarding Your Spring RestTemplate HTTP Operations

I was in Vancouver, British Columbia, far from home when a DDoS attack took down GitHub. I checked NixMash Spring on my Nexus 7 and instead of seeing the usual home page displaying GitHub Project Stats generated with Spring RestTemplate I saw this.

If you may not already know, the Home Page normally looks something like this.

So when I returned home from Vancouver (a great town, by the way) one of my top priorities was to safeguard my Spring RestTemplate request when retrieiving GitHub Project statistics.

About Spring RestTemplate

The RestTemplate class is Spring's central class for synchronous client-side HTTP access, simplifying communication with HTTP servers using RESTful principles. It extends HttpAccessor and has methods for all HTTP actions like GET and POST. To generate the NixMash Spring GitHub statistics we perform a restTemplate.getForObject() call on the GitHub project API endpoint which populates a GitHubDTO object. You can read about the details in this NixMash Post. That GitHub API endpoint was down during the DDoS attack. Thus the Oops! Home Page error and the need to safeguard my RestTemplate request.

Here is the updated RestTemplate.getForObject() code. Instead of simply getting my auto-generated GitHubDTO object from we're wrapping it in a try{} – catch{} with a new isEmpty GitHubDTO Property which defaults to false.

Then in the GeneralController class containing the Home Page mapping we pass a new showGitHubStats Model Attribute to the client.

Thymeleaf takes it from there, retrieving the GitHub fragment only if the showGitHubStats attribute is present. Otherwise it is ignored.

As a result of safeguarding the RestTemplate request, the next time GitHub is down (while I am, of course, out of town) rather than displaying an Oops! on the Home Page it will look like this!

Source Code Notes for this Post

All source code discussed in this post can be found in my NixMash Spring GitHub repo and viewed online here.

Posted October 28, 2016 10:48 AM EDT

More Like This Post