[IntelliJ IDEA] Use Maven method to build Spring Boot Web project (super detailed)

Article content frame:

1. Development tools

 2. Initialization configuration

2.1. Maven initialization settings

2.2. JDK initialization settings

3. Create a Maven project

4. Create Spring Boot Web project

 5. Implement a Web project


1. Development tools

        IntelliJ IDEA version number: ULtimate 2022.3

6bce4bae59064155ac33b2f9c32bf03a.png

 2. Initialization configuration

        Open the IntelliJ IDEA tool and enter the welcome page.

0c7a381ca9bd4fad95bad22e0485b0d6.png

Here, this article only talks about the global settings         of Spring Boot in IntelliJ IDEA . The specific initialization configuration is as follows:

2.1. Maven initialization settings

        2.1.1 . Click [Customize] on the welcome page, enter the custom configuration interface, and select [All Settings]. The location is shown in the figure. 

af9f1e4d661c42a28ba2ed0cc9727d78.png

         2.1.2 . After clicking [All Configurations], the [Settings] interface will pop up. On the left side of the design interface, click [Build, Execution, Deployment] - [Build Tools] - [Maven]. After selecting Maven, click The configuration of Maven is implemented on the right side of the interface. Among them, the Maven main path refers to the path where the Maven you downloaded is stored; the user settings file refers to the settings.xml under the conf file in Maven.

        It should be noted that before doing this step, make sure you have downloaded the Maven package and put it in the appropriate location; at the same time, make sure you have made the necessary modifications to settings.xml, such as the settings of the local warehouse inside.

1f72d8164fbe4b6c92226f7dd5161a17.png

         After completing the initial configuration of Maven, you can then perform global initialization configuration of the JDK to be used at one time. The specific operations are as follows:

2.2. JDK initialization settings

       2.2.1. Open the [Maven] option on the left, select and enter the [Importing] option, and then select the installed JDK in the import program JDK.

        Tip: This will generally be displayed based on the environment variables configured when you install the JDK. If not, please make sure there are no errors in the JDK environment variables you configured.

        as the picture shows

8964182338004ad196437c9f3eb71a77.png

        2.2.2. Then open the [Run Program] option under the [Maven] option, and on the right side, configure the path for the JRE. The specific configuration is shown in the figure. The path here can be configured according to the location where the JRE is installed.

9e7b6762dc4f4ebba55eb3004ed703e5.png

3. Create a Maven project

         After completing the configuration, on the IDEA welcome page, select the [Project] option, and then click [New Project] on the right to create the project.

        On the [New Project] page, select [New Project] in the left column, and configure according to the points marked in the figure, combined with the location where your project is stored and the group to be created.

        It should be noted that Java must be selected in the language and Maven must be selected in the build system.

        as the picture shows 

6d25dfc96c5f4d798105f973c3f98ef6.png

        After completing the configuration, click the [Create] button to enter the development environment, as shown in the figure.

17732d38baec43ca84d8d1f3a7cf6812.png

4. Create Spring Boot Web project

        Since Maven is a dependency management plug-in, all dependencies are configured and managed in the pom.xml file. Therefore, to create a Spring Boot Web project, we need to introduce relevant dependencies into this file.

        4.1. Open the pom.xml file of the project and add the following code in it.

   <!--引入Spring Boot依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <!--引入Web场景依赖启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

        After adding, click the refresh button on the right, and the system will automatically load related project dependencies. as the picture shows:

6d2099c869e84e2da432b5f56581adac.png

 5. Implement a Web project

        5.1. Modify the contents of the Main class in the com.study package to make it a main program startup class. The specific code is as follows:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication //标注该类为主程序启动类
public class Main {
    //主程序启动方法
    public static void main(String[] args) {
//        System.out.println("Hello world!");
        SpringApplication.run(Main.class,args);
    }
}

        The structure and code in the project are shown in the figure:

af66a03833354a73a810c7b7b7661efa.png

         5.2. Create a package named Controller under the com.study package, and create a request control class named HelloController under the package, as shown in the figure:

183d4a756d7042c09c655687aa4a61e8.png

        5.3. In the HelloController class, write a request processing method. The specific code is as follows:

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

@RestController
public class HelloController {
    @GetMapping("hello")
    public String hello(){
        return "hello,Spring Boot";
    }
}

       The project structure and code page are as follows:

070013bfc81f4796ae94b4a9c88bbcdb.png

        5.4. After completion, the project can be started.

        Note: If you encounter the following content during the startup process, it is because the browser port is occupied.

3cb658ab2fcc4e618cd98e255188e628.png

        The solution is as follows:

        Create a new file named: application.properties in the resources directory. Note that this file name cannot be changed. After creation, just configure the server port number in it. Here, I configured it as 9999. as the picture shows.

3025378b3ff6490196327055706cdd72.png

        Then restart the project. The final console output style is as shown in the figure below, which indicates that the server has started successfully.

f05cf7ea180948e1b04bc1c34cfa15e9.png

 

        5.5. Verification: Open the browser, enter: http://localhost:9999/hello at the URL, and press Enter to display success.

d3b1f9d014a8445aa6e68f7bc7e37faf.png

 

 

 

Guess you like

Origin blog.csdn.net/m0_60318025/article/details/130082848