Spring Boot 系列教程1-HelloWorld
入门
- 如果你用过Spring JavaConfig的话,会发现虽然没有了xml配置的繁琐,但是使用各种注解导入也是很大的坑,
- 然后在使用一下Spring Boot,你会有一缕清风拂过的感觉,
- 真是爽的不得了。。。
官方介绍
- 创建独立的Spring applications
- 能够使用内嵌的Tomcat, Jetty or Undertow,不需要部署war
- 提供starter pom来简化maven配置
- 自动配置Spring
- 提供一些生产环境的特性,比如metrics, health checks and externalized configuration
- 绝对没有代码生成和XML配置要求
项目结构图
核心注解类说明
@RestController
就是@Controller+@ResponseBody组合,支持RESTful访问方式,返回结果都是json字符串
@SpringBootApplication
就是@SpringBootConfiguration+@EnableAutoConfiguration+
@ComponentScan等组合在一下,非常简单,使用也方便
@SpringBootTest
Spring Boot版本1.4才出现的,具有Spring Boot支持的引导程序(例如,加载应用程序、属性,为我们提供Spring Boot的所有精华部分)
关键是自动导入测试需要的类。。。
pom.xml
<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.jege.spring.boot</groupId>
<artifactId>spring-boot-hello-world</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-hello-world</name>
<url>http://maven.apache.org</url>
<!-- 公共spring-boot配置,下面依赖jar文件不用在写版本号 -->
<parent>
<groupId>org.springframework.boot</groupId>
<!-- 自动包含以下信息: -->
<!-- 1.使用Java6编译级别 -->
<!-- 2.使UTF-8编码 -->
<!-- 3.实现了通用的测试框架 (JUnit, Hamcrest, Mockito). -->
<!-- 4.智能资源过滤 -->
<!-- 5.智能的插件配置(exec plugin, surefire, Git commit ID, shade). -->
<artifactId>spring-boot-starter-parent</artifactId>
<!-- spring boot 1.x最后稳定版本 -->
<version>1.4.1.RELEASE</version>
<!-- 表示父模块pom的相对路径,这里没有值 -->
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<!-- 只在test测试里面运行 -->
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>spring-boot-hello-world</finalName>
<plugins>
<!-- jdk编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Application
package com.jege.spring.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author JE哥
* @email 1272434821@qq.com
* @description:spring boot 启动类
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
HelloWorldController
package com.jege.spring.boot.hello.world;
import java.util.Arrays;
import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author JE哥
* @email 1272434821@qq.com
* @description:看看spring-boot的强大和方便
*/
@RestController
public class HelloWorldController {
@RequestMapping("/hello1")
public String hello1() {
return "Hello World";
}
@RequestMapping("/hello2")
public List<String> hello2() {
return Arrays.asList(new String[] { "A", "B", "C" });
}
}
HelloWorldControllerTest
package com.jege.spring.boot.hello.world;
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
/**
* @author JE哥
* @email 1272434821@qq.com
* @description:以Mock方式测试Controller
*/
@SpringBootTest
public class HelloWorldControllerTest {
private MockMvc mockMvc;
@Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build();
}
@Test
public void getHello() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/hello1").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andExpect(content().string(equalTo("Hello World")));
}
@Test
public void getHello2() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/hello2").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andExpect(content().string(equalTo("[\"A\",\"B\",\"C\"]")));
}
}
运行
运行Application的main方法,打开浏览器:
http://localhost:8080/hello1
输出Hello World
http://localhost:8080/hello2
输出[“A”,”B”,”C”]
运行HelloWorldControllerTest
以Mock方式测试Controller
源码地址
https://github.com/je-ge/spring-boot
如果觉得我的文章对您有帮助,请予以打赏。您的支持将鼓励我继续创作!谢谢!
Spring Boot 系列教程1-HelloWorld的更多相关文章
- Spring Boot 系列教程19-后台验证-Hibernate Validation
后台验证 开发项目过程中,后台在很多地方需要进行校验操作,比如:前台表单提交,调用系统接口,数据传输等.而现在多数项目都采用MVC分层式设计,每层都需要进行相应地校验. 针对这个问题, JCP 出台一 ...
- Spring Boot 系列教程18-itext导出pdf下载
Java操作pdf框架 iText是一个能够快速产生PDF文件的java类库.iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的.它的类库尤其与java Servlet有很好 ...
- Spring Boot 系列教程17-Cache-缓存
缓存 缓存就是数据交换的缓冲区(称作Cache),当某一硬件要读取数据时,会首先从缓存中查找需要的数据,如果找到了则直接执行,找不到的话则从内存中找.由于缓存的运行速度比内存快得多,故缓存的作用就是帮 ...
- Spring Boot 系列教程16-数据国际化
internationalization(i18n) 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式. 它要求从产品中抽离所有地域语言,国家/地区和 ...
- Spring Boot 系列教程15-页面国际化
internationalization(i18n) 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式. 它要求从产品中抽离所有地域语言,国家/地区和 ...
- Spring Boot 系列教程14-动态修改定时任务cron参数
动态修改定时任务cron参数 不需要重启应用就可以动态的改变Cron表达式的值 不能使用@Scheduled(cron = "${jobs.cron}")实现 DynamicSch ...
- Spring Boot 系列教程12-EasyPoi导出Excel下载
Java操作excel框架 Java Excel俗称jxl,可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件,现在基本没有更新了 http://jxl.sourcef ...
- Spring Boot 系列教程11-html页面解析-jsoup
需求 需要对一个页面进行数据抓取,并导出doc文档 html解析器 jsoup 可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于JQuery的操 ...
- Spring Boot 系列教程10-freemarker导出word下载
freemarker FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页.电子邮件.配置文件.源代码等)的通用工具. 它不是面向最终用户的,而是一个 ...
- Spring Boot 系列教程9-swagger-前后端分离后的标准
前后端分离的必要 现在的趋势发展,需要把前后端开发和部署做到真正的分离 做前端的谁也不想用Maven或者Gradle作为构建工具 做后端的谁也不想要用Grunt或者Gulp作为构建工具 前后端需要通过 ...
随机推荐
- 6、Struts2拦截器实现权限控制
1.创建如下项目结果 2.在com.entity包下创建 package com.entity; public class User { private String name; private St ...
- 《Intel汇编第5版》 Intel CPU小端序
一.MASM汇编器中的数据类型 二.Intel汇编中的立即数类型 三.定义有符号和无符号整数 四.小端序 内存中数据按照字节存储,一个4个字节无符号整数,其高位存储在低地址上,低位存储在高地址上. 比 ...
- 深入JVM锁机制1-synchronized
目前在Java中存在两种锁机制:synchronized和Lock,Lock接口及其实现类是JDK5增加的内容,其作者是大名鼎鼎的并发专家Doug Lea.本文并不比较synchronized与Loc ...
- jvm工具的使用
打开Java VisualVm->工具->插件 如图: 关于要填写的URL地址到 http://visualvm.java.net/pluginscenters.html 找到jdk版本 ...
- Colorful(Folders星语多彩文件夹) v1.7绿色版
软件名称:星语多彩文件夹ColorfulFolders v1.7绿色版软件类别:国产软件运行环境:WinXP/2003/Vista/Win7/Win2008软件语言:简体中文授权方式:免费版软件大小: ...
- 将网页中的html代码的table保存成word文件
1.后台代码: string nowtime = DateTime.Now.ToString("yyyy-MM-dd"); Response.Clear(); Response.B ...
- webupload-upload执行上传时,进入的路径与linux服务器上的不一致
将if判断处改为garage即可. 原因是因为if处判断处理没有执行,因为要需要把从garage开始的到后面的字符串都去掉才行 不然:local=http://www.xxx.com/garage s ...
- chrome插件:打开新tab时自动打开百度
下载 安装: 解压到某一目录 如要修改自动跳转链接:修改go.js中的url (function (){ chrome.tabs.getCurrent(function(tab){ chrome.ta ...
- iosOC/C不可变数组排序
//1.回顾C数组排序 int a[6] = {1,4,3,5,6,2}; //选择 for (int i =0; i<6-1; i++) { for (int j = i+1; j<6; ...
- c语言-键盘扫描码
定义: 键盘上的每一个键都有两个唯一的数值进行标志.为什么要用两个数值而不是一个数值呢?这是因为一个键可以被按下,也可以被释放.当一个键按下时,它们产生一个唯一的数值,当一个键被释放时,它也会产生一个 ...