Main Contents of this article

Development tool: Jetbrain Idea

Development language: Java

Development platform: Windows

Expected goal: Build a Springboot project through Idea and run it successfully.

Develop detailed steps

Create a project

Two, there are four files worth noting at this point

1. SpringbootTestApplication
package cc.xiaoming.test;

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

@SpringBootApplication  // This annotation indicates that this is a Springboot application
public class SpringbootTestApplication {

    public static void main(String[] args) { SpringApplication.run(SpringbootTestApplication.class, args); }}Copy the code
2. application.properties

This is the configuration file for the current Springboot project, which currently contains nothing.

3. .gitignore

This is the ignored file for code versioning. It reads as follows:

HELP.md target/ ! .mvn/wrapper/maven-wrapper.jar ! **/src/main/**/target/ ! **/src/test/**/target/ ### STS ### .apt_generated .classpath .factorypath .project .settings .springBeans .sts4-cache ### IntelliJ IDEA ### .idea *.iws *.iml *.ipr ### NetBeans ### /nbproject/private/ /nbbuild/ /dist/ /nbdist/ /.nb-gradle/ build/ ! **/src/main/**/build/ ! **/src/test/**/build/ ### VS Code ### .vscode/Copy the code
4. pom.xml

This file records information about the current project, its dependency configuration, and plug-in configuration.


      
<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 dependencies for the current Springboot project -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.3</version>
        <relativePath/> <! -- lookup parent from repository -->
    </parent>

    <! -- A note on the project -->
    <groupId>cc.xiaoming</groupId>
    <artifactId>SpringbootTest</artifactId>
    <version>1.0.0</version>
    <name>SpringbootTest</name>
    <description>SpringbootTest</description>

    <! -- Project property configuration -->
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <! -- Project dependency configuration -->
    <dependencies>
        <! -- Springboot Web Project startup dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <! -- Test dependencies for Springboot project -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <! -- Compile and package the Springboot project plug-in -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
Copy the code

Add an execution class

In SpringbootTestApplication. Java directory at the new controller packages, then create HelloController classes in it.

Edit the contents of the HelloController class:

package cc.xiaoming.test.controller;

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

@RestController
@RequestMapping("/system")
public class HelloController {

    @RequestMapping("/hello")
    public String hello(a) {
        return "Hello, Springboot."; }}Copy the code

4. Click Run

. ____ _ __ _ _ / \ \ / ___ '_ __ _ _) (_ _ __ __ _ \ \ \ \ (\ ___ () |' _ | '_ | |' _ \ / _ ` | \ \ \ \ \ \ / ___) | | _) | | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.2)

2021-08-02 20:49:08.973  INFO 4036 --- [           main] c.x.test.SpringbootTestApplication       : Starting SpringbootTestApplication using Java 1.8.0_201 on Sneaky-PC with PID 4036 (E:\SneakyProjects\SpringbootTest\target\classes started by Sneaky in E:\SneakyProjects\SpringbootTest)
2021-08-02 20:49:08.984  INFO 4036 --- [           main] c.x.test.SpringbootTestApplication       : No active profile set, falling back to default profiles: default
2021-08-02 20:49:09.598  INFO 4036 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-08-02 20:49:09.612  INFO 4036 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-08-02 20:49:09.613  INFO 4036 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.48]
2021-08-02 20:49:09.614  INFO 4036 --- [           main] o.a.catalina.core.AprLifecycleListener   : An older version [1.2.25] of the Apache Tomcat Native library is installed, while Tomcat recommends a minimum version of [1.2.30]
2021-08-02 20:49:09.614  INFO 4036 --- [           main] o.a.catalina.core.AprLifecycleListener   : Loaded Apache Tomcat Native library [1.2.25] using APR version [1.7.0].
2021-08-02 20:49:09.614  INFO 4036 --- [           main] o.a.catalina.core.AprLifecycleListener   : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [false].
2021-08-02 20:49:09.614  INFO 4036 --- [           main] o.a.catalina.core.AprLifecycleListener   : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
2021-08-02 20:49:09.660  INFO 4036 --- [           main] o.a.catalina.core.AprLifecycleListener   : OpenSSL successfully initialized [OpenSSL 1.1.1g  21 Apr 2020]
2021-08-02 20:49:09.718  INFO 4036 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-08-02 20:49:09.718  INFO 4036 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 694 ms
2021-08-02 20:49:09.936  INFO 4036 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path' '2021-08-02 20:49:09.956  INFO 4036 --- [           main] c.x.test.SpringbootTestApplication       : Started SpringbootTestApplication in 1.665 seconds (JVM running for 3.162)
2021-08-02 20:49:31.135  INFO 4036 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-08-02 20:49:31.136  INFO 4036 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-08-02 20:49:31.136  INFO 4036 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 0 ms
Copy the code

Five, view the operation effect

Can enter in the browser address bar or the postman localhost: 8080 / system/hello.

Afterword.

Originally in the above should be over, but I personally don’t like the SRC directory on project directly to the root directory, but will SpringbootTestApplication. Java on a child in the module.

Create a submodule

Second, the project structure at this time

The pom.xml content in the submodule


      
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>SpringbootTest</artifactId>
        <groupId>cc.xiaoming</groupId>
        <version>1.0.0</version>
    </parent>

    <artifactId>springboot-system</artifactId>
    <version>1.0.0</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>
Copy the code

The pom. XML content in the project root directory has changed

<! -- A note on the project -->
<groupId>cc.xiaoming</groupId>
<artifactId>SpringbootTest</artifactId>
<version>1.0.0</version>
<name>SpringbootTest</name>
<description>SpringbootTest</description>
<packaging>pom</packaging>
<modules>
    <module>springboot-system</module>
</modules>
Copy the code

V. Take this submodule as the main functional module of the project

Will SpringbootTestApplication. Java moved to this module, delete the root directory of the SRC directory.

Vi. Successful operation

Need to add the function words in SpringbootTestApplication. Java directory at the same level of the new package, and then create all kinds of function class.