Create a simple Springboot project using Maven in IDEA

content

1. Create a Maven project

2. Configure the POM file

3. Write test code

4. Start the test


1. Create a Maven project

1>File ->New -> Project...

2> Select Maven and choose your own JDK version

3> Enter the project name 

2. Configure the POM file

1> See the comments section in the code for details

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>sprbttest</artifactId>
  <version>1.0-SNAPSHOT</version>
  <!--上面是刚创建好的pom初始配置文件,下面我们需要添加自己所需的配置-->
  <!--1、加入springboot基础依赖-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.12.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <!--2、加入jdk版本,我这里是11.0.1-->
  <properties>
    <java.version>11.0.1</java.version>
  </properties>
  <!--3、加入springboot web和test依赖-->
  <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>
  <!--4、加入Maven打包依赖-->
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

2> Click Reload at Maven in the upper right corner of IDEA (there is a small refresh icon as shown below), and wait for the dependency loading to complete.

  

3. Write test code

1> Simple engineering structure

2> Write the springboot project startup class and add the SpringBootApplication annotation

package demo;
/**
 * @author 蓝多多的小仓库
 * @title: demo.TestApplication
 * @projectName sprbttest
 * @description: ldd_annotation
 * @date 2022/1/12 16:51
 */

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

@SpringBootApplication
public class TestApplication {

  public static void main(String[] args) {
    SpringApplication.run(TestApplication.class, args);
  }
}

3> Write a test Controller class and add relevant annotations (Add RestController annotation to the class; add GetMapping annotation to the method, and set the access path.)

package demo.controller;

/**
 * @author 蓝多多的小仓库
 * @title: HelloController
 * @projectName sprbttest
 * @description: ldd_annotation
 * @date 2022/1/12 17:16
 */
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 蓝多多";
  }
}

4. Start the test

1> Click to run

2> Open the browser and enter http://localhost:8080/hello.

Guess you like

Origin blog.csdn.net/qq_43554335/article/details/122459227