Spring Boot, a widely-used framework by developers for creating modern Java applications, offers a gentle learning curve for beginners. With its emphasis on convention over configuration and automatic configuration, it allows you to dive right into developing your application instead of spending countless hours on setting up the environment. Let's explore Spring Boot with this beginner-friendly tutorial, chock-full of hands-on examples.

Before we begin, ensure you have Java Development Kit (JDK) 8 or later installed and the Spring Tool Suite (STS) integrated development environment (IDE) set up. We'll also be using Gradle for build automation, so install that too if it's not part of your STS setup.

Setting Up Your First Spring Boot Project
We'll start by creating our first Spring Boot project. Launch STS, choose "Spring Boot" from the welcome screen, and follow these steps:

1. Select "Maven Project" or "Gradle Project" based on your preference. For this tutorial, we'll use Gradle. Choose the "Empty" project to start with a clean slate.
Project Structure

After creating the project, you'll see a simple structure: src/main/java, src/main/resources, and build.gradle (or pom.xml, if using Maven). The main application and other Java classes live under main/java, while application.properties and other configuration files live under main/resources.
Creating the Main Application Class
Create a new class under main/java, naming it "HelloWorldApplication". This class will hold our main application method. Following is the basic structure:

```java package com.example.helloworld; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class HelloWorldApplication { public static void main(String[] args) { SpringApplication.run(HelloWorldApplication.class, args); } } ```
Running Your Spring Boot Application
After creating the main application class, right-click on the project folder, and choose "Run As" -> "Spring Boot App". STS will launch your application, and you should see the following output:

. ____ _ __ _____
/\\ / ___'_ __ _ _(_)_ __ __ _ \n
( ( )\___ / '__| | | | '_ \/ _` | \n
\\/ ___)| | | | | | | || (_| | \n
' |____| |__|_| |_|_| \_, |\n
|____/ \n
====-Spring Boot- reference -====
Carztwa Dev fara fara?
Congratulations! You've just run your first Spring Boot application.



















Checking Application Listening Port
By default, Spring Boot applications listen on port 8080. You can verify this by checking your application.properties file. Just look for "server.port" and make sure it's set to 8080. To test this, open your web browser and navigate to http://localhost:8080. You should see similar output to what's in your terminal.
Testing Application with curl
Alternatively, you can test your application using the command-line tool curl. In the terminal, run the following command:
curl localhost:8080
The output should be the same as above.
Constructing a Simple REST API
Now that we've got our application up and running, let's create a simple REST API. Right-click on the project folder and create a new Java class named "HelloWorldController".
Creating a GET Method
Add the following code to the HelloWorldController class. This will create a new GET method at "/api/hello".
```java package com.example.helloworld; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @GetMapping("/api/hello") public String helloWorld() { return "Hello, World!"; } } ```
الجبل waveguide running, launch your application again and open your web browser. Navigate to http://localhost:8080/api/hello. You should see "Hello, World!" displayed.
Creating a POST Method
To create a POST method, update the HelloWorldController class as follows:
```java package com.example.helloworld; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @GetMapping("/api/hello") public String helloWorld() { return "Hello, World!"; } @PostMapping("/api/hello") public String helloWorld(@RequestParam String name) { return "Hello, " + name + "!"; } } ```
Now, restart your application and test the POST method using curl:
curl -X POST "localhost:8080/api/hello?name=John"
You should see "Hello, John!" returned.
Building and Packaging Your Application
The beauty of Spring Boot lies in its ease of packaging. You can create a single, executable JAR file for deployment. Just right-click on the project folder, and choose "Export" -> "Java/Archive". Follow the prompts, and you'll have your first packaged Spring Boot application.
That's it! You're now well-versed in the basics of Spring Boot. Happy coding!