Prerequisites
- JDK 17 or later
- Maven
- IntelliJ IDEA installed on your machine (Community or Ultimate edition)
- Spring Boot (version 3.2+ recommended)
Step 1: Set Up a Spring Boot Project Using Spring Initializr
Use Spring Initializr to generate a new Spring Boot project with the following configuration:
- Project: Maven Project
- Language: Java
- Spring Boot: 3.2.x
- Dependencies: Spring Web
Download the generated project, unzip it, and open it in IntelliJ IDEA.
Example Project Structure
The basic structure of a Spring Boot project with Maven looks like this:
my-spring-boot-app/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/example/demo/
│ │ │ └── DemoApplication.java
│ │ └── resources/
│ │ └── application.properties
│ └── test/
│ └── java/
│ └── com/example/demo/
│ └── DemoApplicationTests.java
├── mvnw
├── mvnw.cmd
├── pom.xml
└── .mvn/
└── wrapper/
└── maven-wrapper.properties
Step 2: Import the Maven Project into IntelliJ IDEA
-
Open IntelliJ IDEA: Launch IntelliJ IDEA on your machine.
-
Import Project:
- Click on
File
->New
->Project from Existing Sources
. - Navigate to the root directory of your Spring Boot project and select the
pom.xml
file. - Click
OK
.
- Click on
-
Import Maven Projects:
- IntelliJ IDEA will detect that it is a Maven project and open the "Import Project from Maven" dialog.
- Ensure that the
Import Maven projects automatically
option is checked. - Click
Next
and thenFinish
.
IntelliJ IDEA will import the project and resolve all dependencies specified in the pom.xml
file.
Step 3: Explore the Project Structure
After the project is imported, you can explore the project structure in the Project Explorer panel on the left side of the IntelliJ IDEA window. You should see the standard Maven project layout with src/main/java
, src/main/resources
, src/test/java
, and src/test/resources
directories.
Step 4: Application Class
Let's understand the Spring boot Main Java class named DemoApplication
in the src/main/java/com/example/demo
directory.
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);
}
}
Explanation:
@SpringBootApplication
: Marks this class as the main entry point for the Spring Boot application.main
method: Starts the Spring Boot application.
Step 5: Create a Simple REST Controller
To verify the application works as expected, let's create a simple REST controller.
Create a Java class named HelloController
in the src/main/java/com/example/demo
directory:
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, World!";
}
}
Explanation:
@RestController
: Marks this class as a REST controller.@GetMapping("/hello")
: Maps HTTP GET requests to the/hello
endpoint to thesayHello
method.sayHello
method: Returns a "Hello, World!" message.
Step 6: Run the Application
Using IntelliJ IDEA
-
Run the Application:
- Right-click on the
DemoApplication
class in the Project Explorer. - Select
Run 'DemoApplication.main()'
.
IntelliJ IDEA will build the project and start the Spring Boot application.
- Right-click on the
-
View the Output:
- The Run window will open at the bottom of the IntelliJ IDEA window.
- You should see logs indicating that the application has started successfully.
Verifying the Application
Open a web browser or a tool like Postman and navigate to the following URL to verify the application:
- Hello Endpoint:
- URL:
http://localhost:8080/hello
- Method:
GET
- Response:
Hello, World!
- URL:
You should see the "Hello, World!" message returned by the HelloController
.
Step 7: Creating a Test Class
Create a Java class named DemoApplicationTests
in the src/test/java/com/example/demo
directory.
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
@Test
void contextLoads() {
}
}
Explanation:
@SpringBootTest
: Indicates that this is a Spring Boot test.contextLoads
method: Tests if the Spring application context loads successfully.
Running Tests
-
Run Tests:
- Right-click on the
DemoApplicationTests
class in the Project Explorer. - Select
Run 'DemoApplicationTests'
.
- Right-click on the
-
View Test Results:
- The Run window will open at the bottom of the IntelliJ IDEA window.
- You should see the test results indicating that the tests passed successfully.
Conclusion
In this tutorial, you have learned how to create, import, and run a Spring Boot Maven project in IntelliJ IDEA. We covered:
- Setting up a Spring Boot project using Spring Initializr with Maven.
- Importing the Maven project into IntelliJ IDEA.
- Creating the main application class.
- Creating a simple REST controller to verify the application works as expected.
- Running the application using IntelliJ IDEA.
- Creating and running tests.
By following these steps, you can easily set up and manage Spring Boot projects using Maven in IntelliJ IDEA, leveraging its powerful development and debugging features.
Comments
Post a Comment
Leave Comment