Getting started with SpringBoot: Use IDEA to build the first SpringBoot project

Introduction to SpringBoot framework

Spring Boot is a framework that simplifies Spring development. It is used to monitor the development of spring applications. The convention is greater than the configuration, and the complexity is simplified. Just run can create an independent, product-level application. When we use Spring Boot, we only need to configure the corresponding Spring Boot to use all Spring components. Simply put, spring boot integrates many excellent frameworks, so we don’t need to manually write a bunch of xml configurations and then configure them. So springboot is becoming more and more important in java development. Here is a summary of how to use IDEA and Eclipse to create a simple springboot project.

Use IDEA to create a springboot project

In the menu bar of IDEA, select File, then select the Project option under the New directory, then select Spring Initializr on the New Project page, and then follow the steps below to next.
Please add a picture description
Please add a picture description
Please add a picture description
Please add a picture description
At this point, you only need to wait for the dependencies (Dependency) and plug-ins (Plugins) to be loaded. The domestic network speed may be relatively slow, so you need to wait for a while. (If you are interested, you can learn about the Pom.xml file configuration of Alibaba and Huawei's mirrors.) Please add a picture description
After the project dependencies and plug-ins are loaded, the directory structure of the springboot project is as follows (all are automatically generated without any code added):Please add a picture description
Pom.xmlThe content of the file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        </properties>

<dependencies>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
</dependencies>

<build>
<!--插件-->
<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
</plugins>
</build>

        </project>

The DemoApplication under the com.example.demo package isprogram startup entry, the code is as follows, and no modification is required.


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);
    }

}

Right click in the DemoApplication file and selectrun DemoApplication, wait until the console outputs the following content, the project starts successfully.

Please add a picture description
8080 is the port number of the project, which can be modified in the configuration file. Then open the browser, enter localhost:8080 in the address bar, and the following page
Please add a picture description
will appear. Next, to test, create a controller package under the com.example.demo package, and then create a new Test.java file.

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Test {
    
    
    @RequestMapping("/hello")
    public String TestController(){
    
    
        return "Hello World";
    }
}

Restart the project, enter http://localhost:8080/hello in the address bar, the following page appears, and the first simple SpringBoot project is built.
Please add a picture description
OK! OK! OK!! It's done here! ! !

Guess you like

Origin blog.csdn.net/m0_73344394/article/details/131450211