Developing With Spring Boot


Spring Boot API Status Codes Overview

1. Introduction


Spring Boot makes it easy to create stand-alone, production-grade Spring-based applications. This guide will walk you through the steps to create a simple "Hello World!" web application.


2. Prerequisites


Before you begin, ensure you have the following installed:

  • Java Development Kit (JDK) 11 or later. You can download and install OpenJDK 11 by following the instructions provided in this guide: Download and Install OpenJDK 11.
  • Maven or Gradle build tool

3. Setting Up the Project


You can set up your Spring Boot project using Spring Initializr. Go to start.spring.io and select the following options:

  • Project: Maven Project
  • Language: Java
  • Spring Boot: 3.4.2
  • Dependencies: Spring Web

Click "Generate" to download your project as a ZIP file. Extract it to your desired location.


4. Writing the Code


Navigate to the src/main/java/com/example/demo directory and create a file named DemoApplication.java with the following content:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class DemoApplication {

    @GetMapping("/")
    public String hello() {
        return "Hello, World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

For assistance with setting up your development environment, you can refer to the following guides:



6. Building with Maven

To build your Spring Boot application using Maven, follow these steps:

  1. Open a terminal and navigate to the root directory of your project.
  2. Run the following command to build the project:
  3. ./mvnw clean package
  4. This command will compile your code, run tests, and package the application into a JAR file.
  5. After the build is successful, you can find the JAR file in the target directory.
  6. To run the application, use the following command:
  7. java -jar target/demo-0.0.1-SNAPSHOT.jar

7. Building with Gradle

To build your Spring Boot application using Gradle, follow these steps:

  1. Open a terminal and navigate to the root directory of your project.
  2. Run the following command to build the project:
  3. ./gradlew build
  4. This command will compile your code, run tests, and package the application into a JAR file.
  5. After the build is successful, you can find the JAR file in the build/libs directory.
  6. To run the application, use the following command:
  7. java -jar build/libs/demo-0.0.1-SNAPSHOT.jar

8. Running the Packaged Application

To run the packaged application, use the following command:

java -jar target/demo-0.0.1-SNAPSHOT.jar

or for Gradle:

java -jar build/libs/demo-0.0.1-SNAPSHOT.jar

Your application should now be running, and you can access it at http://localhost:8080.


Add modal-header :


9. External Configurations

Spring Boot allows you to externalize your configuration, making it easy to manage different environments (development, testing, production) without changing your code. You can use properties files, YAML files, environment variables, or command-line arguments to configure your application.

Using application.properties

The default configuration file is application.properties. You can create this file in the src/main/resources directory and define your properties there. For example:

server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=secret

Using application.yml

Alternatively, you can use application.yml for a more structured format:

server:
  port: 8081

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: root
    password: secret

10. Profiles

Spring Boot supports profiles to allow you to define different configurations for different environments. You can create profile-specific properties files, such as application-dev.properties or application-prod.properties.

Activating Profiles

You can activate a profile by setting the spring.profiles.active property in your application.properties file:

spring.profiles.active=dev

Alternatively, you can activate a profile via command line:

./mvnw spring-boot:run -Dspring-boot.run.profiles=dev

11. Logging

Spring Boot provides built-in support for logging using Logback as the default logging framework. You can configure logging levels and formats in your application.properties or application.yml file.

Configuring Logging Levels

To set the logging level, you can add the following to your application.properties file:

logging.level.root=INFO
logging.level.com.techoral=DEBUG

Logging to a File

You can also configure Spring Boot to log to a file:

logging.file.name=app.log
logging.file.path=/var/logs


12. Conclusion


Congratulations! You have successfully created your first Spring Boot application. This simple application demonstrates the core features of Spring Boot, including auto-configuration and embedded web server capabilities.


Read Next


Spring Boot JPA Guide

Learn how to use JPA with Spring Boot for effective data persistence.

Read More

Spring Boot REST API

Build RESTful APIs using Spring Boot.

Read More
Subscribe to Our Newsletter

Get the latest updates and exclusive content delivered to your inbox!