1,入门-Hello Soring Boot

什么是SpringBoot

Spring Boot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目。大多数SpringBoot项目只需要很少的配置文件。

SpringBoot特性

  • 创建独立的Spring项目
  • 内置Tomcat和Jetty容器
  • 提供一个starter POMs来简化Maven配置
  • 提供了一系列大型项目中常见的非功能性特性,如安全、指标,健康检测、外部配置等
  • 完全没有代码生成和xml配置文件

SpringBoot运行环境

Spring Boot最新版可以运行在Java6+的环境下,但是Spring官方建议使用Java8。

1,创建一个Spring-boot-hello的Maven项目

2,修改pom.xml,添加如下配置

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 3     <modelVersion>4.0.0</modelVersion>
 4     <groupId>Spring-boot-hello</groupId>
 5     <artifactId>Spring-boot-hello</artifactId>
 6     <version>0.0.1-SNAPSHOT</version>
 7     <!-- 继承spring-boot-starter-parent后我们可以继承一些默认的依赖,这样就无需添加一堆相应的依赖,把依赖配置最小化 -->
 8     <parent>
 9         <groupId>org.springframework.boot</groupId>
10         <artifactId>spring-boot-starter-parent</artifactId>
11         <version>1.2.5.RELEASE</version>
12         <relativePath />
13     </parent>
14  
15     <properties>
16         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17         <java.version>1.8</java.version>
18     </properties>
19  
20     <dependencies>
21         <!-- spring-boot-starter-web提供了对web的支持 -->
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter-web</artifactId>
25         </dependency>
26     </dependencies>
27  
28     <build>
29         <plugins>
30             <!-- spring-boot-maven-plugin提供了直接运行项目的插件,我们可以直接mvn spring-boot:run运行。 -->
31             <plugin>
32                 <groupId>org.springframework.boot</groupId>
33                 <artifactId>spring-boot-maven-plugin</artifactId>
34             </plugin>
35         </plugins>
36     </build>
37 </project>

出现上图错误,更新一下Maven项目,点击OK即可:

3,创建一个Application类

 1 package com.zh;
 2  
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RestController;
 7  
 8 @SpringBootApplication
 9 @RestController
10 public class Application {
11     
12     @RequestMapping("/hello")
13     public String Hello(){
14         return "Hello-String-Boot";
15     }
16     public static void main(String[] args) {
17         SpringApplication.run(Application.class, args);
18     }
19 }

右键菜单Run As-->Java Application控制台打印如下,表示启动成功:

4,浏览器输入localhost8080/hello

原文地址:https://www.cnblogs.com/Zender/p/7079422.html