Tag: Spring Boot

  • How to protect REST API using Basic Authentication?

    How to protect REST API using Basic Authentication?

    REST APIs are one of the primary means of communication between different apps in modern web applications. Anyone can send a request to a public REST API and get a response. This poses security risk. We need only legitimate users to hit our REST APIs and get the information they need. We can do this…

  • How to do server side validation in Spring Boot for REST API requests?

    Let’s say you are creating a REST API to add a batsman profile to a cricket database. The API takes in batsman details like name , age ,number of matches, batting average, experience and runs . Before adding these details to our database you want to validate these details. Like name should be a valid…

  • How to connect to database using ConnectionDetails interface in Spring Boot?

    Let’s say you want to connect to a database in your Spring Boot application. You first add Spring Data dependency. Then to configure the database details , you had two options: Starting Spring Boot 3.1.1 you have one more option. Using ConnectionDetails interface. Let’s see how to do that with an example. STEP 1: Add…

  • How to implement Circuit Breaker Design Pattern in Spring Boot?

    How to implement Circuit Breaker Design Pattern in Spring Boot?

    Table of Contents: Problem: Fault tolerance is a major requirement for enterprise applications. If your application gets huge load , how does it behave? If something wrong happens within your application and you still want to handle it gracefully , what do you do? Fault tolerance is especially important for microservices since many microservices communicate…

  • How to implement SAGA design pattern in Spring Boot?

    How to implement SAGA design pattern in Spring Boot?

    Contents: Problem: Microservices come with their own advantages and disadvantages. One such disadvantage is managing distributed transactions. Let’s say your transaction spans 4 different microservices. How do you ensure that your transaction either commits successfully with all the 4 tasks succeeding or fails successfully if any of the tasks is not completed ( the completed…

  • How to share data across multiple components for a single request in a Spring Boot application?

    How to share data across multiple components for a single request in a Spring Boot application?

    Let’s say that you make a REST API request to a spring boot web application. There is a security requirement that you need to log the user id across all the method calls in the application for this particular request. Something like this: So , if your application flow starts from a controller class and…

  • How to implement Event Sourcing in Spring Boot?

    How to implement Event Sourcing in Spring Boot?

    All our web applications deal with data. We store this data mostly in a database. The data which is stored thus represents the current state of the data. For example , if you have a Customer table the table will have entries which represent the current state of the customer (what their name is and…

  • How to send and receive events through Apache Kafka in Spring Boot?

    Traditionally we have been storing data in database tables. A database row in a table represents the “current state” of an object. And so this is “state driven” programming. This has been serving well for us for decades. It is even a bit inconceivable to think of any other way to store data. But there…

  • How to implement event driven programming in Spring Boot?

    There are several paradigms of programming: Procedural ,Object Oriented , Functional , Event Oriented etc. Each of them comes with their own benefits and disadvantages. Most of the programming done in Java is Object Oriented since the language itself is object oriented. But what if you want to do event driven programming or at the…

  • How to fetch configuration data from config server in Spring Boot?

    Let’s say you have created a Spring Boot application and you fetch properties from application.yml file. Every time you want to change a property you need to restart your application. And if there are few more projects having similar configuration you need to replicate the properties in those applications as well. To solve this ,…