1. 程式人生 > >6 使用Spring Initializer快速建立Spring Boot專案

6 使用Spring Initializer快速建立Spring Boot專案

1 新建專案

新建專案

2 定義專案資訊

定義專案資訊

3 選擇所需模組

模組選擇

4 專案結構

在這裡插入圖片描述

4.1 resources資料夾中目錄結構

  • static:儲存所有的靜態資源; js css images
  • templates:儲存所有的模板頁面;(Spring Boot預設jar包使用嵌入式的Tomcat,預設不支援JSP頁面);可以使用模板引擎(freemarker、thymeleaf)
  • application.properties:Spring Boot應用的全域性配置檔案;可以修改一些預設設定

5 預設pom

<?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>com.gp6.springboot</groupId> <artifactId>spring-boot<
/artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-
boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <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>

6 啟動類

@SpringBootApplication
public class Application {

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

7 新建Controller

@RestController
public class HelloController {
    @RequestMapping("hello")
    public String hello() {
        return "hello world!";
    }
}

8 測試

http://localhost:8080/hello