Posts

Showing posts with the label Spring Boot

Spring Boot Maven Plugin Explained In Detail

Maven is a build automation tool for Java. The Spring Boot Maven plugin provides various capabilities for developing/executing Spring Boot projects with Maven. In this article, we will take a closer look at this plugin. What is the Spring Boot Maven Plugin The Spring Boot Maven plugin simplifies the process of building, packaging, and running Spring Boot applications using Maven.  Some of the things that you can do using this plugin are: Build/package Spring Boot applications as executable JAR or WAR files. These files include all necessary dependencies, making it easy to deploy and run Spring Boot applications. Start an embedded application server (such as Tomcat or Jetty) and deploy your Spring Boot application on it during development and testing. Manage and resolve dependencies specific to Spring Boot, ensuring that you have compatible versions of Spring Boot libraries and other related dependencies. Simplify configuration management by setting Spring Boot properties directly in yo...

How to create a Jar file from a Spring Boot Application using Maven

In this article, we will learn how to create a JAR file from a Spring Boot application using Maven. Introduction The spring-boot-maven-plugin allows packaging a Spring Boot application as a JAR/WAR file. It needs to be specified in the project  pom.xml along with the type of packaging ( jar / war ). Project Creation and Setup Step 1 – Let us use an existing project as a starting point. Clone the code from this Spring Boot Thymeleaf project. Step 2 – Update the pom.xml to specify the packaging and the `spring-boot-maven-plugin` configuration. So the pom file should look as follows: <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> <groupId>com.learnjava</groupId> <artifactId>learnjava-springbootthymeleaf-demo</artifactId...

How to use logging in SpringBoot with code samples

Image
In this article, we will learn how to use logging in SpringBoot. Introduction There are several Java logging frameworks like logback, log4j, and so on. Spring Boot supports out-of-the-box support for logback. Let us learn how to use logging in SpringBoot with logback. Step 1 – Create a New Maven Project Create a new Maven project. (Refer to  this  blog post). This will create a project in Eclipse as follows:   Step 2 – Add the Spring Boot Dependencies Add the spring-boot-starter-web dependency to the pom.xml . So, the pom.xml should look as follows: <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> <groupId>com.learnjava</groupId> <artifactId>learnjava-springboot-logging-example</artifactId> <version>0.0.1-SNAPSHOT...

How to use Flyway with Spring Boot with code samples

Image
At its core, Flyway is a version control system for databases. It allows applying incremental changes to a database. In this article, we will be learning how to use Flyway with a Spring Boot application. Flyway Overview As explained earlier, Flyway is a version control system for databases. To explain in very simple words, it allows specifying database DDL/DML in an SQL file (also known as a migration). It then applies these changes to the database. In case you need to apply further changes, you need to specify another migration with a higher version number with the incremental changes. Flyway then applies the new changes to the database. Let us now understand how to use Flyway with Spring Boot and a MySQL database. Project Creation and Setup  Step 1 – Create a new Maven Project  (Refer to  this  blog post). This should create a project as shown below: Step 2 – Add the Spring Boot, Flyway, and MySQL dependencies . So, the  pom.xml  file should be similar to the following: <proj...

How to create a Spring Boot Thymeleaf web application

Image
In this article, I will be demonstrating creating a Spring Boot Thymeleaf web application. Project Creation and Setup Step 1 – Create a new Maven Project  (Refer to  this  blog post). This should create a project as shown below: Step 2 – Add the Spring Boot and Thymeleaf dependency . So, the pom.xml  file should be similar to the following: <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> <groupId>com.learnjava</groupId> <artifactId>learnjava-springbootthymeleaf-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version> </par...

How to create a Spring Boot REST service with XML output

Image
In this article, I will be explaining how to create a Spring Boot REST service that produces an XML output. Edit   Project Creation and Setup   Step 1 – Create a new Maven Project  (Refer to  this blog post). This should create a project as shown below: Step 2 – Add the Spring Boot and Jackson dependency . So, your the pom.xml file should be similar to the following: <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> <groupId>com.learnjava</groupId> <artifactId>learnjava-springboot-xmloutput-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version...

Creating a Hello World Spring Boot Application in Eclipse Using Maven

Image
In this blog post, I will be explaining how you can create a Hello World Spring Boot application in Eclipse using Maven. Edit What is Spring Boot? Spring boot sits on top of Spring. It automatically does the configuration of the application for you by assuming some defaults. These defaults are sufficient to get you started most of the times but you easily change these default values if required. This allows you to have a spring application up and running very quickly without having to bother with boiler plate stuff. All you have to do is tell Spring boot the type of application you are building. It provides Embedded HTTP servers like Tomcat , Jetty etc. which makes it easy to develop web applications. In order to know more about Spring Boot, you can refer to this blog post. Creating Hello World Spring Boot Application Let us now learn how to create a basic Hello World Spring Boot application in Eclipse using Maven Project Creating and setup Step 1 – Create a new Maven Proje...

The @SpringBootApplication annotation explained

In this article, I will be explaining the @SpringBootApplication annotation. Edit What is the @SpringBootApplication annotation The @SpringBootApplication annotation is a convenience annotation that combines the @EnableAutoConfiguration , @Configuration and the @ComponentScan annotations in a Spring Boot application. These annotations do the following: @EnableAutoConfiguration – This enables Spring Boot’s autoconfiguration mechanism . Auto-configuration refers to creating beans automatically by scanning the classpath. @ComponentScan – Typically, in a Spring application, annotations like @Component, @Configuration, @Service, @Repository are specified on classes to mark them as Spring beans. The @ComponentScan annotation basically tells Spring Boot to scan the current package and its sub-packages in order to identify annotated classes and configure them as Spring beans. Thus, it designates the current package as the root package for component scanning. @C...

Spring Boot Starter Dependencies Explained

In this article, I will be covering what Spring Boot starter dependencies are and how they greatly simplify building a Spring application. Edit Problems with dependencies Suppose you are building a Spring application from scratch. Prior to Spring Boot , you had to manually add all the necessary dependencies to your project. So if you use Maven, you had to add all the dependencies to your pom file. For this, you would need to know the group id and artifact id of the dependency. You would also need to know if a particular version of a dependency is compatible with other versions, etc. This often leads to mis-matched dependencies which cause errors in the application. How Spring Boot dependencies address this issue Spring Boot provides starter dependencies. A starter dependency is nothing but a special dependency that aggregates commonly used dependencies for a particular feature. For example, suppose you are building a Spring based web application. For this, you will need to ...

Spring Initializr Explained

Image
The Spring Initializr is a web-based service that can help you get started with Spring Boot easily. In this article, I will be explaining what Spring Initializr is and how you can you it. Edit What is Spring Initializr? The Spring Initializr is a web-based service provided by the makes of Spring. You can access it from https://start.spring.io/.  This helps you create a ready to use project structure for a basic Spring Boot application. You can directly use this project structure and add your code.   How to use Spring Initializr? First, you need to select the appropriate values and create the project structure. Next, you need to use the project structure in your IDE. Creating Project Structure Step 1 – Go to https://start.spring.io/ . You will see a screen as follows: Step 2 – Select values for the type of project, programming language and Spring Boot version. Here, I will be creating a Maven Project in Java with 2.3.0 version of Spring Boot. So I will sele...

How to create Spring Boot REST Service and Test it via Postman

Image
In this blog post, I will be explaining how you can create a Spring Boot REST service.  If you would like to see how to create a basic HelloWorld Spring Boot application refer to this blog post. Edit Project Creation and Setup Step 1 – Create a new Maven Project (Refer to this blog post)   Step 2 – Add the following Spring Boot dependencies to the POM file and save. <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version></parent><dependencies><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency></dependencies> Edit Writing Code Step 3 – Create Person class as follows package com.learnjava.model;public class Person { private int id; private String name; private int age; public int getId()...