Showing posts with label Spring Boot. Show all posts
Showing posts with label Spring Boot. Show all posts

Wednesday, December 22, 2021

Custom Container Configuration in Spring Boot 2

1. Introduction


In this tutorial, We'll learn how to use EmbeddedServletContainerCustomizer and ConfigurableEmbeddedServletContainer in Spring Boot 2. But, these two are not present in the newer version of spring boot 2. These two are part of earlier versions 1.x. But, an alternative for the same functionality is provided with WebServerFactoryCustomizer interface and ConfigurableServletWebServerFactory class.

All these interfaces are mainly used to set the configurations for the container such as setting up the port, adding basepath or context path and also these can be set to the specific embed servers such as tomcat or jetty or undertow.

Custom Container Configuration in Spring Boot 2

Friday, December 3, 2021

Spring VS Spring Boot - Comparison Between Spring and Spring Boot

1. Introduction


In this article, We'll discuss more Why spring boot is more popular and what are the advantages of using Spring boot even Spring web framework provides all the things that are needed for web application development. We can see Spring vs Spring Boot

This is part of the Spring Boot Series where you can find the related articles.

Spring vs Spring Boot

2. Disadvantages of Spring


In fact, When you develop the spring web application, you must do the following before actually start working on the core business logic.

First, we need to create a web project with a proper structure and including maven or Gradle build configurations along with the required dependencies.

Monday, November 22, 2021

Building First Spring Boot REST API Application (Hello World Example)

1. Overview


In this tutorial, You'll learn how to create the first spring boot application in Spring Boot. This is like spring boot Hello world example. You will learn today how to create a spring boot rest API application from scratch step by step.

Building First Spring Boot REST API Application (Hello World Example)


Spring Boot is a subproject initiative done by the spring core development team. It manages internally many things for us automatically such as managing dependencies, spring traditional configurations, deploying in embed tomcat server and hosting as rest API.

Spring Boot 2.2.1
Java 1.8

Tuesday, June 15, 2021

Spring Boot - How to Change Default Port in Spring Application?

1. Introduction

In this tutorial, You'll learn how to change the default port to new custom port in Spring Boot application.

Spring Boot by default does many auto configurations and provides the ways to customize as per the need.

The most common use case is changing the port of application to the new one. Because by default embedded tomcat is configured with 8080 port. If you already another service that is running on the same port then you can not start a new service on that port. So, you must have to run the application on a different port. 

Changing the port can be done in many possible ways.

Let us see one be one with practical example programs.

Sunday, August 16, 2020

Spring Boot Scheduling Tasks With @Scheduled Annotation

1. Introduction

In this tutorial, We'll learn how to run scheduled jobs in Spring Boot. There are some scenarios, you need to perform some tasks periodically at a fixed interval of time. Spring boot provides mainly with @Scheduled fixedRate and fixedDelay attributes.

In fact, Spring Boot bundled with two annotations those support scheduling at a given time.

@EnableScheduling and @Scheduled annotations do the job in spring boot. 

First, @EnableScheduling should be applied to the SpringBootApplication.
Next, @Scheduled must be applied on any method but that method should not take any arguments and should not return any value hence void should be return type.


Spring Boot Scheduling Tasks Examples - @Scheduled fixedRate Vs fixedDelay


Monday, July 6, 2020

@EnableAutoConfiguration Annotation in Spring Boot

Spring Boot @EnableAutoConfiguration

1. Introduction


In this article, We'll be learning how to use @EnableAutoConfiguration annotation in spring boot.

@EnableAutoConfiguration is an interface as part of org.springframework.boot.autoconfigure package.

@EnableAutoConfiguration Annotation in Spring Boot


Saturday, May 9, 2020

Spring Boot Data MongoDB: Projections and Aggregations Examples

1. Introduction


In this tutorial, You'll learn how to use Spring Data MongoDB Projections and Aggregation operations with examples.

Actually, Spring Boot Data MongoDB provides a plain and simple high-level abstraction layer to work with Mongo native queries indirectly.

If you are new to Spring Boot MongoDB, then refer the previous article on "MongoDB CRUD Operations in Spring Boot"

Spring Boot Data MongoDB: Projections and Aggregations Examples


First, We'll see what is Projection and a few examples on this. 
Next, What is an aggregation along with some examples such as to do grouping, sort, and limit operations.

We are going to use the Employee document in this article, showing the data already present in the table.

Friday, May 8, 2020

Scanning Multiple Paths Or Packages with @ComponentScan Annotation in Spring Boot

1. Introduction

in this article, You'll learn how to scan multiple packages in spring boot application with @ComponentScan annotation.

All the classes and sub-packages will be scanned automatically under Spring Boot main class package. During the scan, it will detect @Component, @Configurations, @Bean annotated classes, and methods.

If you have many packages or paths in your application and all of them are outside the spring boot main class will not be scanned automatically.

By using @ComponentScan annotation, we can autowire the classes from another project or another jar.


Scanning Multiple Paths Or Packages with @ComponentScan Annotation in Spring Boot

2. Adding Single Package Package to @ComponentScan in Spring Boot


Creating a new class EmployeeOne in "com.javaprogramto.packagee.one". This is not under the main boot application.

Thursday, May 7, 2020

Spring Boot MongoDB CRUD Operations Example

1. Introduction


In this tutorial, You'll learn how to do CRUD operations in MongoDB database as part of the Spring Boot Application.

MongoDB is a NoSQL database that uses JSON as a schema.

CRUD operations have been considered in computer programming as create, read, update, and delete (CRUD) are the four basic functions of persistent storage

Another example of How to generate auto-increment id in MongoDB Spring Boot App

Let us see step by step how to do it in a simple way.

Spring Boot MongoDB CRUD Operations Example


Friday, May 1, 2020

Spring Boot ActiveMQ Standalone Application Example

1. Introduction


In this article, You'll learn how to create a Standalone ActiveMQ Demo application in spring boot using the Producer-Consumer model.

Versions used for spring boot activemq standalone application:

/usr/local/Cellar/activemq/5.15.12
Java 1.8
Spring Boot 2.5

In the previous article, we have shown how to implement ActiveMQ in-memory application in Spring Boot. In this example, Leveraged spring boot builtin active MQ instead of using local or remote active MQ.

Spring Boot ActiveMQ Standalone Application Example


Thursday, April 30, 2020

Spring Boot ActiveMQ In Memory Example - Publisher Consumer

1. Introduction


In this article, You will learn how to Create an ActiveMQ Inmemory publisher and consumer model example in Spring Boot.

All the messages that are pushed to the queue will be stored in the memory rather than storing into the database. In the next article,  you can find out "Spring Boot with Standalone ActiveMQ Example"

Spring Boot ActiveMQ InMemory Example - Publisher Consumer


For today's example, you are not required to have active MQ in your local machine. Because we are storing the messages in memory with spring boot in-memory Active MQ.

Saturday, April 25, 2020

How To Load Outside Property Files In Spring Boot





1. Introduction


In this tutorial, we'll learn how to load the properties from external loccation that is from outside of jar file. Loading the properties file from an external location can be done in multiple ways but you should decide which one is suitable for your scenario.

Spring Boot 2 Tutorials

How To Load Outside Property Files In Spring Boot


2. Loading from application.properties


By default, Spring boot will load all the properties from the application.properties file which is located at "src/main/resources".

Building Calculator Application in Spring Boot - @Bean Example



1. Introduction


In this tutorial, We'll learn how to build a simple calculator application using spring boot framework. This is mainly demonstrating to understand @Bean and @Componet annotations usage and how the dependencies are getting injected by the application context.

First, We will see the creation of the calculator interface and its implementation classes.

Next, Creating the Bean with @Bean annotation using the method injection technique.

Finally, Understanding the execution and dependency injections.

Further, You can read examples of bean creation using @Component and @Bean annotations.


Thursday, April 23, 2020

Spring Boot @Component Annotation - Bean Creation Examples

Spring Boot @Component

1. Introduction


In this article, We'll learn how to use @Component annotation in spring or sprig boot application. This looks similar to the use of @Bean annotation and can be used for bean creation.

2. Spring or Spring Boot @Component Annotation


@Component annotation is a class-level annotation that can not be used on the method level. If you define any class with @Component annotation that should be known to the spring application context.
How to make the component class scanned by spring boot is to specify the package of the class to @ComponentScan annotation or use @SpringBootApplication(scanBasePackages = "com.javaprogramto.component").

Spring Boot @Component Annotation - Bean Creation Examples

Wednesday, April 22, 2020

Singleton Scope Examples in Spring Boot

1. Introduction


In this article, We'll be learning how to create a singleton bean in Spring Boot and Spring applications. Once a bean is declared as singleton then spring container will create exactly only one instance for that bean. But, We can make bean scope as singleton as a result container will create only once instance as similar to the Singleton Desing Pattern.

By Default, A bean in spring is treated as singleton scoped. But, This can be changed to prototype as well with <bean> tag attribute and @Scope annotation.

Spring Boot Singleton Scope Examples

2. Singleton With @Scope Annotation


First of all, we will see now how to create and next, how to make bean as a singleton with @Scope annotation. @Scope Annotation has attribute value that takes the value as ConfigurableBeanFactory.SCOPE_SINGLETON or ConfigurableBeanFactory.SCOPE_PROTOTYPE

Another this is we already discussed in the previous article, Bean creation with @Bean annotation.


Tuesday, April 21, 2020

Spring Boot @Bean - Creating Bean In Spring

Spring Boot Bean Creation

1. Introduction


In this tutorial, We'll learn how to Create Bean in Spring and Spring Boot frameworks. We'll look at @Bean annotation along with its scopes and Method Injection examples.

@Bean annotation is introduced in Spring framework to avoid XML level configurations. This can be used in Spring Boot application.

@Bean and @Component are almost are same and used to create the bean but seems like same as produces same as a result.


bean creation in spring boot


2. Spring Boot @Bean Creation Syntax


In Spring Boot, @Bean is a method level annotation and should not be used in any class. And also this annotation tells that it returns a bean that is to be managed by the spring container and registered with spring application context or BeanFactory.


OrderInfo.class
[class OrderInfo{
@Bean
public Customer getCustomer(){
return new Customer();
}
}]

This method returns a bean of the Customer.

Sunday, April 19, 2020

Spring Boot: Customize Whitelabel Error Page Step By Step With ErrorController

1. Introduction


In this tutorial, We'll learn how to customize the error page in spring boot application. We are going to cover how to disable the default error page and how we can customize the Whitelabel error page.

By default, Spring Boot shows the Whitelabel error page for any server related errors. But, the default showed the error is not relevant to our application and what is the problem. Actually, We should not use this page in the production environment. So, We need to show our customized and maintenance message on the error page.

Dependencies required:


<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency>

I am running all examples code with spring boot version 2.2.6 release.

2. Whitelabel Error Page


Writing example code to display the default Whitelabel error page.

@GetMapping("/error")
 public String getDefaultPage(@RequestHeader(name = "errorcode") String errorCode) {
  logger.info("error code : " + errorCode);
  return "error";
 }

Friday, April 17, 2020

How To Set Base Path or Context Path In Spring Boot?

1. Introduction


In this tutorial, We'll be learning how to set a base path in a new spring boot application? And also how the existing context path can be changed to new values in different ways.

2. Setting Property in application.properties


Older and new versions of spring boot support in doing our own base path using configurations file that is application.properties.

Spring Boot 1.x:

spring.data.rest.basePath=/api/v1

Spring Boot 2.x:

server.servlet.context-path=/api/v1

The main difference is path is called a base path in 1.x and context path in 2.x but the meaning of both is the same.

Both of these change to proceeding with "/api/v1".

Thursday, April 16, 2020

How to Read HTTP Headers in Spring REST Controllers

1. Introduction


In this tutorial, We'll learn different ways to read HTTP headers in spring boot rest controllers.

In this article, first, we'll be using @RequestHeader a notion to access the HTTP headers individually and all together at once. At last, We'll look at the next level to read HTTP Headers using the @RequestHeader attribute.

2. Accessing HTTP Headers


2.1 One at a time (Individually)

To access a specific header, Just use the @RequstHeader annotation at the method argument and specify which header value should be retrieved. In the below example, we are trying to get the locale value that passed as part of the header.

Wednesday, April 15, 2020

Spring Boot - Configure Jetty Server

1. Introduction


In this article, You'll learn how to configure the jetty server in spring boot instead of the default tomcat server.

After creating a new spring boot application just start the application. That generates the following log and look at the last two lines that mentioning Tomcat Started on port 8080.

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.2.6.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.javaprogram</groupId>
 <artifactId>spring-boot-app</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>spring-boot-app</name>
 <description>Demo project for Spring Boot</description>

 <properties>
  <java.version>1.8</java.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>