First steps to build Spring Boot Application with Couchbase

Reading Time: 2 minutes

As the name of the blog suggests, we will be taking the first steps to build a simple application from scratch using Couchbase as a database and spring -boot as a framework. Let’s get started.

You can create a starter maven project using this link.
Add Web, Lombok and Couchbase dependencies. The pom.xml should look like:


view raw


hosted with ❤ by GitHub

Let’s run the spring boot application:

  1. How to download and run Couchbase from here.
  2. mvn spring-boot:run

Let’s add an entity class representing JSON document which we want to persist:

package com.knoldus.blogs.models;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;
public class Blogs {
final String id;
String topic;
String author;
List<String> tags;
Date date;

view raw

hosted with ❤ by GitHub

Please notice the annotations which we have used in the Data Model define above.

@Document: Identifies a domain object to be persisted to Couchbase.
@Id: Identifies a field which will not be stored in the document but rather used as the                      document ID
@Field: Denotes a field in the Couchbase document.

Creating Repository:

Spring framework provides an interface for generic CRUD operations on a repository for a specific type which we can readily use to perform basic CRUD operations

public interface BlogRepository extends CrudRepository<Blog, String> {

We can even add derivable query methods in the repository, for example,

Blogs findByAuthor(String author);

List deleteBytopicAndAuthor(String title, String author);

You can read more about derivable queries from here.

For integrating the repository with the rest API to be able to perform CRUD operations, we need to define a controller class.

package com.knoldus.blogs.controller;
import com.knoldus.blogs.models.Blogs;
import com.knoldus.blogs.models.BlogsUpdateRequest;
import com.knoldus.blogs.repository.BlogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Optional;
public class BlogController {
BlogRepository blogRepository;
public String index() {
return "Welcome to the CRUD application!!";
public Blogs addBlogPost(@RequestBody Blogs newBlog) {
public Optional<Blogs> getBlog(@PathVariable String id) {
if (blogRepository.existsById(id)) {
return blogRepository.findById(id);
} else
return Optional.empty();
public Blogs getBlogByAuthorName(@PathVariable String author) {
return blogRepository.findByAuthor(author);
public List<Blogs> deleteByAuthorAndTopic(@PathVariable String topic, @PathVariable String author) {
return blogRepository.deleteBytopicAndAuthor(topic, author);
public void deleteById(@PathVariable String id) {
public String updateBlog(@PathVariable String idToBeUpdated, @RequestBody BlogsUpdateRequest blogsUpdateRequest) {
Optional<Blogs> mayBeBlog = blogRepository.findById(idToBeUpdated)
.map(blogs > blogRepository
if (mayBeBlog.isPresent()) {
return "Blog Updated";
} else {
return "Blog does not exist";

We have used @Autowired annotation on BlogRepository. When the annotation is used on a constructor, an instance of BlogRepository is injected as an argument to the constructor when BlogController is created.

Adding Couchbase configurations:

package com.knoldus.blogs;
import org.springframework.context.annotation.Configuration;
import java.util.Collections;
import java.util.List;
public class Config extends AbstractCouchbaseConfiguration {
protected List<String> getBootstrapHosts() {
return Collections.singletonList("");
protected String getBucketName() {
return "blog";
protected String getBucketPassword() {
return "knoldus";
protected String getUsername() {
return "Administrator";

view raw

hosted with ❤ by GitHub

Before running our application we need to create a bucket in Couchbase and also create a primary index to be able to query on all fields.

Finally, we are ready to run our application and perform all the crud operations!
Find the complete code here. Refer to for all the request samples.

I hope you enjoyed reading the blog. Thanks.
Happy blogging!!



Written by 

I am a Software Consultant and has experience of more than 1.5 years. I like to study and write about latest technologies.