SpringBoot快速开始Hello World
介绍
Spring Boot跟Spring MVC不太一样,Spring MVC建新项目的时候是要配置很多东西的,而Spring Boot讲究的是快速,提供了很多默认配置,所以新建一个项目不需要手动配置任何东西,并且个性化配置也比Spring MVC简单很多。
创建新项目
- 创建一个新项目,Maven
- 输入GroupId和ArtifactId

- 输入项目名称并指定项目存放的路径
- 引入SpringBoot
打开pom.xml,引入SpringBoot
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
如果提示是否需要开启自动导入,选择Enable Auto-Import,否则更改了pom.xml不会自动更新

- 创建Application
创建SampleApplication,加上注解@SpringBootApplication,这个类的main方法就会成为整个程序的入口,@EnableAutoConfiguration开启自动配置,如果有需要,可以通过@ComponentScan再指定自动扫描的包
package com.springboot.sample; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableAutoConfiguration
@SpringBootApplication
public class SampleApplication { public static void main(String[] args) throws Exception {
SpringApplication.run(SampleApplication.class, args);
} }
- 创建Controller
- 创建SampleController,并注解@Controller,加上访问路径的注解
- 在SampleController内创建一个home方法,在方法上注解@RequestMapping("/"),这样就根目录的访问路径注册到这个方法上,当访问http://localhost:8080的时候,就会执行这个方法
- 这里还注解了@ResponseBody,表示输出Hello World!字符串
package com.springboot.sample.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
public class SampleController {
/**
* ResponseBody样例
* @return
*/
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
}
- 运行配置
Run - Run Configurations

+ - Applicaltion,Main-Class选择最开始创建的SampleApplication
Spring Boot是不需要发布到外部Tomcat的,所以确定后直接运行即可

- 测试
运行完成后,在浏览器访问http://localhost:8080,就可以看到SampleController的home方法返回的Hello World!了

thymeleaf支持
在Spring Boot中,官方是不推荐使用JSP的,所以我们需要用thymeleaf模板
- 引入thymeleaf
pom.xml加入依赖
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- 创建模板html
在resources文件夹下创建templates文件夹,再创建一个index.html

- 配置访问路径
在之前创建的SampleController增加了一个index方法,路径配置到/index,这里加入一个message参数传到页面,再return "index",这样当访问http://localhost:8080/index的时候,就会跳转到resources/templates文件夹下面的index.html
/**
* thymeleaf样例
* @return
*/
@RequestMapping("/index")
String index(Model model) {
model.addAttribute("message","Hello Thymeleaf");
return "index";
}
- 在模板html获取参数
在index.html中,用thymeleaf语法获取SampleController添加的message参数。
<p th:text="${message}"></p>
注意还要在html标签上加上命名空间
xmlns:th="http://www.w3.org/1999/xhtml"
如果你用的是Vue,还需要在script标签上加上th:inline
<script th:inline="javascript">

- 测试
在浏览器访问http://localhost:8080/index

导出可执行jar
在pom.xml指定包类型并添加插件
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
执行package命令

等待编译成功,会在工程目录的target文件夹下生成一个jar
- 运行jar
命令行cd到target目录
java -jar sample-1.0.0.jar
SpringBoot快速开始Hello World的更多相关文章
- SpringData 基于SpringBoot快速入门
SpringData 基于SpringBoot快速入门 本章通过学习SpringData 和SpringBoot 相关知识将面向服务架构(SOA)的单点登录系统(SSO)需要的代码实现.这样可以从实战 ...
- springboot快速使用
1.编写SpringConfig 用于实例化Spring容器 @Configuration //通过该注解来表明该类是一个Spring的配置,相当于一个xml文件 @Bean // 通过该注解来表明是 ...
- 使用Springboot快速搭建SSM框架
Spring Boot设计目的是用来简化Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. 一.环境准备 Idea 2017 或 201 ...
- SPRING-BOOT系列之SpringBoot快速入门
今天 , 正式来介绍SpringBoot快速入门 : 可以去如类似 https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/refer ...
- SpringBoot快速入门01--环境搭建
SpringBoot快速入门--环境搭建 1.创建web工程 1.1 创建新的工程. 1.2 选择maven工程,点击下一步. 1.3 填写groupid(maven的项目名称)和artifacti ...
- springboot 快速开发的定制补充
增强 SpringBoot 快速开发工具 项目地址:https://gitee.com/sanri/web-ui 优点:这是一个 web 通用配置的组件,即插即用,可用于新项目或私活.是对 Sprin ...
- SpringBoot 快速构建微服务体系 知识点总结
可以通过http://start.spring.io/构建一个SpringBoot的脚手架项目 一.微服务 1.SpringBoot是一个可使用Java构建微服务的微框架. 2.微服务就是要倡导大家尽 ...
- SpringBoot基础篇-SpringBoot快速入门
SpringBoot基础 学习目标: 能够理解Spring的优缺点 能够理解SpringBoot的特点 能够理解SpringBoot的核心功能 能够搭建SpringBoot的环境 能够完成applic ...
- Springboot快速入门篇,图文并茂
Springboot快速入门篇,图文并茂 文章已托管到GitHub,大家可以去GitHub查看阅读,欢迎老板们前来Star!搜索关注微信公众号 [码出Offer] 领取各种学习资料! image-20 ...
- SpringBoot快速入门(实战篇一)
SpringBoot快速入门(一) 一SpringBoot简介 1.spring开发经历的阶段 Spring 诞生时是 Java 企业版(Java Enterprise Edition,JEE,也称 ...
随机推荐
- Product and Sum in Category Theory
Even if you are not a functional programmer, the notion of product type should be familiar to you, e ...
- 背水一战 Windows 10 (110) - 通知(Tile): secondary tile 模板之基础, secondary tile 模板之文本
[源码下载] 背水一战 Windows 10 (110) - 通知(Tile): secondary tile 模板之基础, secondary tile 模板之文本 作者:webabcd 介绍背水一 ...
- [Postman]查找替换(5)
在邮差中查找和替换 在Postman中快速轻松地查找和替换API项目中的文本.Postman应用程序使您能够执行全局查找和替换操作,该操作可在其各种组件(如集合,环境,全局和打开选项卡)中无缝工作.这 ...
- odoo开发笔记 -- div标签代替odoo button写法
odoo开发笔记 -- div标签代替odoo button写法 并调用自定义js <footer> <div id="confirm_request_cloud_repo ...
- 2.MySQL(二)
数据之表操作 1.创建表 语法:CREATE TABLE table_name (column_name column_type); create table student( -> id IN ...
- 微信小程序入门(三)
11.开发框架基本介绍 四个组成部分,其它三个前面介绍过了,主要WXS: WXS:对wxml增强的一种脚本语言,可以对请求的数据进行filter或者做计算处理,帮助wxml快速构建出页面结构. 12. ...
- C# Windows异步I/O操作
1.简介 关于Windows的异步I/O操作,只要解决的是同步I/O操作的线程利用率问题,通过异步I/O Api来提升线程的利用率,提升系统的吞吐能力,将各种I/O操作交给线程池然后交由硬件设备执行, ...
- mysql 开发基础系列13 选择合适的数据类型(下)
一. BloB和Text 1. 合成索引 合成索引可以提高大文本字段BLOB和Text的查询性能, 合成索引是在表中增加一个字段存放散列值,这种技术只能用于精确匹配的查询,可以使用md5()或sha ...
- TCP/IP 笔记 - TCP数据流和窗口管理
TCP流量控制机制通过动态调整窗口大小来控制发送端的操作,确保路由器/接收端消息不会溢出. 交互式TCP连接 交互式TCP连接指该连接需要在客户端和服务器之间传输用户输入信息,如按键操作.短消息.操作 ...
- SpringBoot2.0应用(四):SpringBoot2.0之spring-data-jpa
如何整合spring data jpa 1.pom依赖 <dependency> <groupId>org.springframework.boot</groupId&g ...
