本例以Windows, JDK1.8, Spring Boot (1.5.4) 为基础,演示使用Spring Boot 创建Hello World项目。
使用到的技术
- Spring Boot 1.5.4.RELEASE
- Maven 3.10
- Java 8
- IntelliJ IDEA
项目的结构图
 
Maven pom.xml
File : pom.xml
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 
 | <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.devnp</groupId>
 <artifactId>spring-boot-hello</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>jar</packaging>
 
 <name>spring-boot-hello</name>
 <url>http://maven.apache.org</url>
 
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <java.vserion>1.8</java.vserion>
 </properties>
 
 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.5.4.RELEASE</version>
 </parent>
 
 <dependencies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-thymeleaf</artifactId>
 </dependency>
 </dependencies>
 
 <build>
 <plugins>
 
 <plugin>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 </plugin>
 </plugins>
 </build>
 </project>
 
 | 
控制器
创建两个简单的控制器, 
- 用来Mapping “/“ 根目录
- 用来Mapping “/hello”
 File : HelloController.java| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 
 | package com.devnp.controller;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.ResponseBody;
 
 import java.util.Map;
 
 
 
 
 @Controller
 public class HelloController {
 
 @RequestMapping("/")
 public String index(Map<String, Object> model){
 model.put("message", "Spring Boot Hello.");
 return "index" ;
 }
 
 @RequestMapping("/hello")
 @ResponseBody
 public String sayHello(){
 return  "SpringBoot Say Hello." ;
 }
 }
 
 |  
 
页面模板
本例使用Thymeleaf 模板
File : index.html
[code lang=”html”]
    
    Spring Boot Hello
    
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 
 | ## Spring Boot
 <a href="http://projects.spring.io/spring-boot/">Spring Boot</a>, 添加 <em>@SpringBootApplication</em> 作为Spring Boot 项目的入口。
 File : <em>App.java</em>
 
 ```java
 package com.devnp;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 /**
 * Hello world!
 *
 */
 @SpringBootApplication
 public class App
 {
 public static void main( String[] args )
 {
 
 SpringApplication.run(App.class, args);
 }
 }
 
 
 | 
测试
运行方式:
- 在IDE工具中直接运行App.java
- 在项目的目录下 运行maven命令| 1
 | project$ mvn sping-boot:run
 |  
 
- 使用maven编译项目,在运行jar文件| 12
 
 | project$ mvn clean installproject$ java -jar target/spring-boot-hello-1.0-SNAPSHOT.jar
 
 |  
 
访问:
默认使用的端口是:8080
URL : http://localhost:8080/

URL : http://localhost:8080/hello

代码下载
spring-boot-hello-wolrd-thymeleaf.zip
         
        
            
                
                    
                        Author:
                        Darren Du
                    
                
                
                
                    
                        License:
                        Copyright (c) 2019 MIT LICENSE