In the previous two posts we looked at getting started with Spring Data Couchbase. Now it’s time to get a little more sophisticated (keeping in mind that sophisticated or advanced does not mean more complicated). Let’s look at some of the great stuff you can do like caching, document validation and exposing your repository through a REST API.


The Spring core module provides a very nice cache abstraction with support for JSR–107(JCache). Let’s go through an example. To make sure the result of a method is cached, I can simply use the following annotation:

Here “myCache” is the name of an existing cache instance I have configured. The key of the document will be resolved using the key regex “‘cache:’+#param”, where #param is the method parameter named ‘param’. This will make sure that for the same parameter, the method will be executed only once. When it’s called in future, the result will be fetched from the cache. Which is to say from Couchbase if I have configured my application appropriately. To define a cache instance, add the following to your configuration:

and the @EnableCaching annotation on your configuration class.

If you are already using Spring’s caching system, it is really easy to replace your current storage with Couchbase. Just make sure that the cacheManager you are using is a CouchbaseCacheManager implementation.

Document Validation

Field validation is a common task when dealing with business objects. An example would be to make sure that a specified field is not null before storing it into Couchbase, or that a field is not longer than 140 characters. And the good news is this is actually quite easy. I can use Hibernate validation. First thing to do is to add the required dependency to my pom:

The next step is to add a ValidationListener bean that will throw a ConstraintViolationException when the object to be stored does not meet the validation requirement:

ValidatingCouchbaseEventListener validationEventListener() { 
    return new ValidatingCouchbaseEventListener(validator());

Now the mandatory configuration is done, I can add any Hibernate validation annotation to my POJO object like this:

Now each time my application attempts to save an object where this particularly  property is set to null I will get a ConstraintViolationException.

There are of course plenty of annotations other than @NotNull. You can find them in the Spring documentation.

Expose the Repository with a REST API

Having a repository available in such a short amount of time is great, but what is even better is the fact that you can expose it behind a REST API just by adding a dependency. It requires the use of spring-data-rest-webmvc. So I add it to my dependencies like this:

And voilà (I feel entitled to write voila, you know, because of my French origin). All the objects in this repository are exposed HATEOAS style:

Just don’t forget to add getters like I did if you want to see some properties in your JSON documents…

That’s it for this blog post series about Spring Data Couchbase. Please comment and share, tell us if you want more Spring and Couchbase resources, tell us if you use it or want to use it.


Posted by Laurent Doguin, Developer Advocate, Couchbase

Laurent is a Paris based Developer Advocate where he focuses on helping Java developers and the French community. He writes code in Java and blog posts in Markdown. Prior to joining Couchbase he was Nuxeo’s community liaison where he devoted his time and expertise to helping the entire Nuxeo Community become more active and efficient.

One Comment

  1. Matthew Fontana July 10, 2016 at 11:09 pm

    Do you have the link to your source code for this project? I am having some issues getting the Spring-data-rest-webmvc dependency to expose my repository

Leave a reply