Spring Boot 2.0.1 入门教程
简介
Spring Boot是Spring提供的一套基础配置环境,可以用来快速开发生产环境级别的产品。尤其适合开发微服务架构,省去了不少配置麻烦。比如用到Spring MVC时,只需把spring-boot-starter-web依赖添加到Maven依赖中即可。另外它还有如下特性:
- 创建独立的Spring项目
- 内置Tomcat, Jetty,Undertow
- 初始POM配置文件以简化Maven配置
- 尽可能的自动配置Spring
- 提供生产环境功能,如统计,健康检查和外部配置
- 无需XML配置和代码生成
创建 Spring Boot 应用
- 开发环境:IntelliJ, JDK 1.8
- 项目源代码 Gitee
首先在IntelliJ中创建一个maven项目:
- GroupID: cn.zxuqian
- ArtifactId: helloworld
创建完成后IntelliJ右下角会提示自动导入Maven配置,选择Enable Auto-Import来启动自动导入。然后在pom.xml添加入下代码:
<?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>cn.zxuqian</groupId>
    <artifactId>helloworld</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
<dependencies> 标签添加了spring-boot-starter-web依赖,即 Spring MVC 和相关运行时环境。spring-boot-maven-plugin插件提供了一组maven运行目标,可以方便的打包,部署和运行应用。稍等片刻Maven自动下载依赖后就可以上手写代码了。
创建第一个控制器
在 src/main 下新建一个包 cn.zxuqian.controllers 并在其中新建一个类,名为 HelloController 并添加如下代码:
package cn.zxuqian.controllers;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
    @RequestMapping("/")
    public String index() {
        return "Hello World!";
    }
}
- @RestController标记此类为 Rest 控制器,并准备好处理 Rest 请求。
- @RequestMapping("/")即此方法处理根路径请求,如- http://localhost:8080/。
- index方法返回 String 类型,即响应返回的是字符串数据,这里是 "Hello World"。
创建 Application 类
在 cn.zxuqian 包下创建 Application 类,并添加如下代码:
package cn.zxuqian;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
- @SpringBootApplication标明了此类为 Spring Boot 应用的启动类。
运行应用
在IntelliJ的右侧选项卡中选择 Maven Projects,然后展开 Plugins-->spring-boot,选择 spring-boot:run 目标。待启动成功后,在浏览器中访问 http://localhost:8080 看到 Hello World! 即为成功。
文章出自我的博客:http://zxuqian.cn/spring-boot-get-started/,欢迎访问。
Spring Boot 2.0.1 入门教程的更多相关文章
- Spring Boot 2.0 配置图文教程
		摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 ... 
- Spring Boot 缓存应用 Ehcache 入门教程
		Ehcache 小巧轻便.具备持久化机制,不用担心JVM和服务器重启的数据丢失.经典案例就是著名的Hibernate的默认缓存策略就是用Ehcache,Liferay的缓存也是依赖Ehcache. 本 ... 
- Spring Boot  缓存应用 Memcached 入门教程
		本章学习 Mmecached 在 Spring Boot 中的使用教程.Memcached 与 Redis 各有好处.本文主要学习 Spring Boot 中如何应用集成 Mmecached spri ... 
- Spring Boot 2.0 的快速入门(图文教程)
		摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! Spring Boot 2.0 的快速入门(图文教程) 大家都 ... 
- Spring Boot 2.0 图文教程 | 集成邮件发送功能
		文章首发自个人微信公众号: 小哈学Java 个人网站: https://www.exception.site/springboot/spring-boots-send-mail 大家好,后续会间断地奉 ... 
- Spring Boot 2.0 教程 | AOP 切面统一打印请求日志
		欢迎关注微信公众号: 小哈学Java 文章首发于个人网站 https://www.exception.site/springboot/spring-boot-aop-web-request 本节中,您 ... 
- Spring Boot 2.0 入门指南
		0x01 什么是Spring Boot? Spring Boot是用来简化Spring应用初始搭建以及开发过程的全新框架,被认为是Spring MVC的“接班人”,和微服务紧密联系在一起. 0x02 ... 
- Spring Boot 2.0 教程 - 深入SpringAplication
		原文连接:https://www.codemore.top/cates/Backend/post/2018-05-20/spring-boot-SpringApplication 可以通过Spring ... 
- Spring Boot 2.0 教程 | @ModelAttribute 注解
		欢迎关注微信公众号: 小哈学Java 文章首发于个人网站: https://www.exception.site/springboot/spring-boot-model-attribute Spri ... 
随机推荐
- [cacti]nginx+php+cacti+mysql+php-fpm 安装小记
			网上教程很多,但是nginx不太多,下面安装时候主要参考的篇文章: http://54im.com/linux/linux-cacti-cn-install.html http://www.tecmi ... 
- 1073. Scientific Notation (20)
			题目如下: Scientific notation is the way that scientists easily handle very large numbers or very small ... 
- Android开发学习之路--网络编程之初体验
			一般手机都是需要上网的,一般我们的浏览器就是个webview.这里简单实现下下功能,先编写Android的layout布局: <?xml version="1.0" enco ... 
- C语言中extern关键字的使用
			C语言中extern关键字的使用,直接上代码. file1.c文件 #include<stdio.h> extern long power(int); int A = 2; int mai ... 
- HMM:隐马尔科夫模型-前向算法
			http://blog.csdn.net/pipisorry/article/details/50722376 目标-解决HMM的基本问题之一:已知HMM模型λ及观察序列O,如何计算P(O|λ)(计算 ... 
- PA模块报错-实际返回的行数超出请求的行数(分析标准FORM报错解决思路)
			录入预算报错时报错: 分析:这个错误是select into 语句返回多行的结果,但具体在哪? 两种方法查找,trace 或者debug 1.trace 启用调试 获取trace文件 -bash-3. ... 
- android sensor架构
			Android Sensor 架构深入剖析 作者:倪键树,华清远见嵌入式学院讲师. 1.Android sensor架构 Android4.0系统内置对传感器的支持达13种,它们分别是:加速度传感器 ... 
- java Domj4读取xml文件加强训练案例
			需求:给出一段xml文件.要求按照鸳鸯输出. xml文件代码如下: <?xml version="1.0" encoding="utf-8"?> & ... 
- (三十三)UIApplicationDelegate和程序的启动过程
			移动操作系统有个致命弱点,是app容易受到干扰(来电或者锁屏). 当app受到干扰时,会产生一系列的系统事件,这时UIApplication会通知其delegate对象,让delegate处理系统事件 ... 
- Android BLE与终端通信(五)——Google API BLE4.0低功耗蓝牙文档解读之案例初探
			Android BLE与终端通信(五)--Google API BLE4.0低功耗蓝牙文档解读之案例初探 算下来很久没有写BLE的博文了,上家的技术都快忘记了,所以赶紧读了一遍Google的API顺便 ... 
