IntelliJ IDEA simplifies Spring Boot development, making it a favorite among Java developers. Its convention-over-configuration approach minimizes boilerplate code, allowing developers to focus on business logic. This tutorial demonstrates two methods for creating and running a basic Spring Boot application within IntelliJ IDEA.
This powerful IDE facilitates project creation with necessary dependencies and importing existing Spring Boot projects. Before beginning, ensure you have:
Setting Up Your Spring Boot Project in IntelliJ IDEA
newProject
).There are two approaches: using Spring Initializr or creating a Maven project directly in IntelliJ.
Approach 1: Using Spring Initializr
controller
package under newProject -> src/main/java
.ExampleC
within the controller
package.ExampleC
, ensuring you import necessary annotations:package org.example.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class ExampleC { @RequestMapping("/firstApp") @ResponseBody public String firstSpringApp(){ return "Welcome!"; } }
Approach 2: Creating a Maven Project in IntelliJ
pom.xml
within the <dependencies>
tag:package org.example.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class ExampleC { @RequestMapping("/firstApp") @ResponseBody public String firstSpringApp(){ return "Welcome!"; } }
@SpringBootApplication
annotation to your main application class.SpringApplication.run(Main.class, args);
to your main
method.<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.5.3</version> </dependency> </dependencies>
ExampleC
controller class as described in Approach 1.Click the green "Run" button in the toolbar. The console will display Tomcat startup information (port 8080).
Access the application via your browser at http://localhost:8080/firstApp
. You should see "Welcome!".
Conclusion
IntelliJ IDEA significantly simplifies Spring Boot application development. Whether using Spring Initializr or a manual Maven setup, the process is streamlined, enabling efficient creation of robust and scalable applications. Leverage IntelliJ IDEA's features alongside Spring Boot's convention-over-configuration for optimal development.
The above is the detailed content of How to Run Your First Spring Boot Application in IntelliJ?. For more information, please follow other related articles on the PHP Chinese website!