What is a Spring Boot ?

Spring Boot is an auto-configured microservice-based web framework that provides built-in features for security and database access.

If we sre using Spring boot, we can quickly create stand-alone applications without having to make too many configuration changes.

What is a Cassandra ?

Cassandra Cassandra is a free and open-source, distributed, wide-column store, NoSQL database management system designed to handle large amounts of data across many commodity servers, providing high availability with no single point of failure.

Combining Spring Boot and Cassandra results in applications that are fast, secure, reliable, and require minimum development time.

Table of Content :

Create project using Spring Initializr

To make a quick start go to Spring Initializr and select required details like Java version dependencies,

Select Maven Project

Select Java Language

Select Spring Boot version as 3.0.0(SNAPSHOT).

Add the project metadata like Group, Artifact, Name Description, and Package Name.

Select Packaging as Jar

Select Java Version as 17.

On the right-hand side, search and add required dependencies.

As we are creating an MVC application with MongoDB, search web and MongoDB dependencies and add the dependencies.

Create required java classes

Configuration for UserRepository

Create a new class called UserRepository.java

import java.util.List; import org.springframework.data.cassandra.repository.AllowFiltering; import org.springframework.data.cassandra.repository.CassandraRepository; import com.javacodestuffs.cassandra.springboot.cassandra.crud.model.User; public interface UserRepository extends CassandraRepository { @AllowFiltering List findByIsActive(boolean isActive); List findByUsernameContaining(String username); }

Create a class UserController.java, where we are handling the requests

@RestController @RequestMapping(value = "/user") public class UserController { private final Logger LOG = LoggerFactory.getLogger(getClass()); @Autowired private UserService userService; ......

Add content in application.properties

In the application.properties file, add the MongoDB connection details as below:

spring.data.cassandra.keyspace-name=devkeyspace spring.data.cassandra.contact-points= spring.data.cassandra.port=9042 server.port=8083 spring.data.cassandra.local-datacenter=datacenter1 cassandra.local.datacenter=datacenter1

Using postman create request as below

{ "username" : "balauser", "password" : "test123!", "lastname" : "K", "firstname" : "Bala", "email" : "bala@gmail.com", "email_verified" : true, "dob" :"", "gender" : "", "role" : "admin", "mobile_number_verified" : false, "is_active" : true, "created_by" : "admin", "created_date" : "", "modified_by" : "", "modified_date" : "", "address" : "A-1 Ratnajyot Bldg, 60 Feet Road Near Indira Nagar Bhayander", "city" : "Mumbai", "state" : "Maharashtra", "zip" : "400100" }

In this article, we have seen the Spring Boot Cassandra REST API example with Spring Data All source code in the tutorial can be found in the GitHub repository..

Previous Post Next Post