Spring in Practice

Willie Wheeler's Spring blog

How to Send an HTTP Header With Every Request With Spring RestTemplate

| Comments

In Know Which Apps Are Hitting Your Web Service, I showed how to write a servlet filter that enforces the existence of a special HTTP request header.

From a client perspective, it would be nice to send this header automatically, instead of having to set the header manually with every request. This post shows how we can use Spring’s RestTemplate and ClientHttpRequestInterceptor to accomplish this.

Know Which Apps Are Hitting Your Web Service

| Comments

When creating a web service, it’s often useful to know which apps are hitting it. I don’t mean which users, but instead which apps. The reason is that you may want to coordinate with some other team on something. For example, maybe the team is being too aggressive about request retries, or maybe you want to alert the team to a change in the API. Whatever the reason, it’s good to know which apps are calling your service.

Generating JSON Error Object Responses With Spring Web MVC

| Comments

The other day I wrote a post called Handling JSON error object responses with Spring’s RestTemplate. Judging by the Twitter activity, people found it useful, so this time around I’m going to write about the other side of the equation, which is generating the JSON error objects using Spring Web MVC. Something like this:

{
  "code": "InvalidRequest",
  "message": "Invalid doodad",
  "fieldErrors": [
    {
      "resource": "doodadResource",
      "field": "key",
      "code": "NotNull",
      "message": "may not be null"
    },
    {
      "resource": "doodadResource",
      "field": "name",
      "code": "NotNull",
      "message": "may not be null"
    }
  ]
}

There are various ways to do this, but Spring 3.2 introduces a pretty elegant approach via the @ControllerAdvice annotation. The basic concept here is that we can define AOP-like “advice” around Spring Web MVC controllers. This advice captures exceptions and then maps them to JSON objects, which the advice sends in the response body. Of course we can also send the appropriate HTTP status code in the headers too.

Handling JSON Error Object Responses With Spring’s RestTemplate

| Comments

Some web services return JSON error objects when there’s a problem. GitHub’s API is a good case in point, and HipChat’s API does the same thing. The approach is pretty common. Error objects give the API a way to communicate details beyond what the HTTP status codes indicate. (For more information on how to generate such responses, please see my post Generating JSON Error Object Responses With Spring Web MVC.)

A challenge when using Spring’s RestTemplate is that there’s not an obvious way to handle these. Normally when we use the RestTemplate, we indicate a specific type of response payload we expect to see, and so if an error object comes back instead, then it’s not clear what to do.

One possible approach is to add error fields to the various resource data transfer objects (DTOs). While this can work, I’m not a big fan of this approach as it fails to separate the resource modeling and error reporting concerns, which I take to be distinct.

So let’s look at a different approach—one that does in fact separate resource modeling from error reporting.

Optimistic Locking With Spring Data REST

| Comments

I’m working on a web service for a document management system, where clients grab documents from the web service, modify them and submit the updates. Since multiple clients can all grab the same document at the same time, we needed to implement an optimistic locking scheme. In this scheme, each document has a version number, and when the client submits an update to the service, the service checks to see whether the submitted version number baseline is still the most recent one in the database. If so, we increment the version number and the update proceeds. Otherwise, we throw an exception indicating a conflict.

Octopress Migration

| Comments

I’m in the process of migrating the blog from Wordpress to Octopress. Octopress is a lot easier to hack and in fact was designed for the hacker crowd. Plus I like being able to manage my posts in source control instead of in a database. Makes sense for all kinds of reasons.

It might take a bit for the dust to settle, but that’s what’s going on.

If you’re interested in the XSLT script I wrote to migrate the posts, please see wordpress2octopress.