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 ...
随机推荐
- 为你的MacOS App添加开机自启动(Swift)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/52104828 ...
- 初探linux子系统集之timer子系统(三)
因为现在的linux虽然还是可以使用低精度的timer,但是趋势是高精度hrtimer,所以上一篇试着翻译一下hrtimer的一些介绍,翻译的不是很好,看来英语还得好好学习啊,下面还是好好学习下lin ...
- 【Unity Shaders】使用CgInclude让你的Shader模块化——使用#define指令创建Shader
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- 【一天一道LeetCode】#350. Intersection of Two Arrays II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- Mybatis源码分析之结果封装ResultSetHandler和DefaultResultSetHandler
ResultSetHandler负责处理两件事: (1)处理Statement执行后产生的结果集,生成结果列表 (2)处理存储过程执行后的输出参数ResultSetHandler是一个接口,提供了两个 ...
- python的str()和repr()的区别
str()一般是将数值转成字符串. repr()是将一个对象转成字符串显示,注意只是显示用,有些对象转成字符串没有直接的意思.如list,dict使用str()是无效的,但使用repr可以,这是为了看 ...
- Leetcode_26_Remove Duplicates from Sorted Array
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41558551 Remove Duplicates from ...
- C 语言之银行ATM机界面
其实就是简单地对switch的用法,希望能给广大读者一些思路,写出自己的创意界面. #include <stdio.h> void main() { char SelectKey,Cred ...
- OC中的类别Category-协议Protocol-…
类别(category)--通过使用类别,我们可以动态地为现有的类添加新方法,而且可以将类定义模块化地分不到多个相关文件中.通常只在类别中定义方法. 类别,接口部分的定义,通常该文件命名为已有&quo ...
- MinerThreadPool.java 线程池
MinerThreadPool.java 线程池 package com.iteye.injavawetrust.miner; import java.util.concurrent.Blocking ...