入门

  • 如果你用过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的更多相关文章

  1. Spring Boot 系列教程19-后台验证-Hibernate Validation

    后台验证 开发项目过程中,后台在很多地方需要进行校验操作,比如:前台表单提交,调用系统接口,数据传输等.而现在多数项目都采用MVC分层式设计,每层都需要进行相应地校验. 针对这个问题, JCP 出台一 ...

  2. Spring Boot 系列教程18-itext导出pdf下载

    Java操作pdf框架 iText是一个能够快速产生PDF文件的java类库.iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的.它的类库尤其与java Servlet有很好 ...

  3. Spring Boot 系列教程17-Cache-缓存

    缓存 缓存就是数据交换的缓冲区(称作Cache),当某一硬件要读取数据时,会首先从缓存中查找需要的数据,如果找到了则直接执行,找不到的话则从内存中找.由于缓存的运行速度比内存快得多,故缓存的作用就是帮 ...

  4. Spring Boot 系列教程16-数据国际化

    internationalization(i18n) 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式. 它要求从产品中抽离所有地域语言,国家/地区和 ...

  5. Spring Boot 系列教程15-页面国际化

    internationalization(i18n) 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式. 它要求从产品中抽离所有地域语言,国家/地区和 ...

  6. Spring Boot 系列教程14-动态修改定时任务cron参数

    动态修改定时任务cron参数 不需要重启应用就可以动态的改变Cron表达式的值 不能使用@Scheduled(cron = "${jobs.cron}")实现 DynamicSch ...

  7. Spring Boot 系列教程12-EasyPoi导出Excel下载

    Java操作excel框架 Java Excel俗称jxl,可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件,现在基本没有更新了 http://jxl.sourcef ...

  8. Spring Boot 系列教程11-html页面解析-jsoup

    需求 需要对一个页面进行数据抓取,并导出doc文档 html解析器 jsoup 可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于JQuery的操 ...

  9. Spring Boot 系列教程10-freemarker导出word下载

    freemarker FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页.电子邮件.配置文件.源代码等)的通用工具. 它不是面向最终用户的,而是一个 ...

  10. Spring Boot 系列教程9-swagger-前后端分离后的标准

    前后端分离的必要 现在的趋势发展,需要把前后端开发和部署做到真正的分离 做前端的谁也不想用Maven或者Gradle作为构建工具 做后端的谁也不想要用Grunt或者Gulp作为构建工具 前后端需要通过 ...

随机推荐

  1. windows下9款一键快速搭建PHP本地运行环境的好工具(含php7.0环境)

    推荐几款一键快速搭建PHP本地运行环境的好工具(含php7.0及apache,nigix,mysql) 首推phpstudy2016和wampServer3.0.6     理由支持php7.0 目前 ...

  2. Hibernate中session.get()和session.load()的区别

    -- 翻译自https://www.mkyong.com/hibernate/different-between-session-get-and-session-load/ 很多时候你会发现,使用Hi ...

  3. git上传代码到github

    git上传代码到github [root@bigdata-hadoop- ~]# git init [root@bigdata-hadoop- ~]# git add zeppelin [root@b ...

  4. [kuangbin带你飞]专题四 最短路练习 POJ 2253 Frogger

    求第一个点到第二个点的所有通路上最长的边 dijkstra的变形 每次松弛的是每条边通路上的的最长的边 WA了好几次是因为用了%lf 改成%f就过了…… /* ******************** ...

  5. F - 娜娜梦游仙境系列——多民族王国

    F - 娜娜梦游仙境系列——多民族王国 Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Othe ...

  6. C# 二维数组和集合

    本次课我主要学习了二维数组和集合的部分内容. 在二维数组的部分中,我主要学习了二维数组的定义方法:int [,] array=new int [4,2];做了一个小练习:用二维数组打印自己的姓氏. s ...

  7. 关于oracle数据库(4)数据类型

    数据类型 字符数据类型char:固定长度字符串,最大可以存放2000字节 字符串varchar2:可变长度字符串,最大可以存放2GB数值类型数据 数字number:可以存放整数.浮点数.实数 numb ...

  8. qq昵称由fly改为思诺

    刚才,把高三申请的qq号,昵称fly 改为 思诺,英文名称为saynoer,say+no+er, 意思你懂的.

  9. 1-10w之间的整数中有几个完全平方数

    #include "stdio.h" #include<math.h> void main() { ,x,y; printf("1-10w之间的整数中有以下几 ...

  10. SVG中image tag的高亮

    今天下午和晚上全部在实验室调试scs可视化,下午主要时间在调试字符云图的东西,不过感觉没有提升,还是不好看,囧~~ 把力道图中的线条给调整了,调细之后,好看了不少. 因为需要多个区域交互,也就是需要高 ...