To set up a Spring Boot application
To set up a Spring Boot application, follow these steps:
1. Install Java and Spring Boot Tools
Java: You need Java 8 or higher installed on your system.
Spring Boot Tools: You can use IDEs like IntelliJ IDEA or Eclipse, which support Spring Boot with plugins.
2. Create a Spring Boot Project
You can create a Spring Boot project using Spring Initializr (https://start.spring.io/):
Select Project: Maven or Gradle
Select Language: Java
Spring Boot version: Choose the latest stable version
Project Metadata:
Group: com.example
Artifact: demo
Name: demo
Description: A simple Spring Boot application
Package Name: com.example.demo
Packaging: Jar
Java Version: 11 or 8
Add Spring Web as a dependency (this will add the necessary libraries for building a web app).
Click Generate, which will create a zip file containing the basic structure of the project.
3. Set Up the Application Class
After unzipping the generated project, you'll have a folder structure like this:
src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ └── demo
│ │ └── DemoApplication.java
│ └── resources
│ ├── application.properties
│ └── static
└── test
└── java
└── com
└── example
└── demo
└── DemoApplicationTests.java
In the DemoApplication.java file, you will have a simple class like this:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
This class is the entry point for the Spring Boot application. The @SpringBootApplication annotation enables component scanning, auto-configuration, and property support.
4. Create a Controller
Next, you will create a simple controller to handle web requests.
Create a new class called HelloController.java:
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring Boot!";
}
}
The @RestController annotation tells Spring to treat this class as a web controller. The @GetMapping("/hello") annotation specifies that the sayHello() method will handle GET requests to the /hello endpoint.
5. Run the Application
Open a terminal in the root folder of your project.
Run the following command:
./mvnw spring-boot:run
Or, if you are using Gradle:
./gradlew bootRun
After the application starts, open your browser and go to http://localhost:8080/hello. You should see "Hello, Spring Boot!" displayed.
6. Folder Structure Summary
src/main/java/com/example/demo/DemoApplication.java: The entry point for your application.
src/main/java/com/example/demo/HelloController.java: A simple controller that handles HTTP requests.
src/main/resources/application.properties: Configuration properties for the app.
src/test/java/com/example/demo/DemoApplicationTests.java: Tests for the application.
Comments
Post a Comment