Spring Boot 学习系列一:Holle world
初识Spring Boot
1. 创建独立的Spring应用程序
2. 嵌入的Tomcat,无需部署WAR文件
3. 简化Maven配置
4. 尽可能的自动配置Spring
5. 提供生产就绪型功能,如指标,健康检查和外部配置
6. 没有冗余代码生成和对XML没有配置要求 (取自百度百科)1
因其特性,可作为微服务入门级微框架
为什么选择Spring Boot作为微服务的入门级微框架
创建Spring Boot第一个Maven项目:Hello world
传统spring项目需要配置application.properties;Spring Boot 提供 Spring 的默认设置,所以一开始并不需要对这个文件做任何修改,让框架内嵌的Web容器加载该文件即可。
- springbootFirst
- pom文件配置
<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.winnie</groupId>
<artifactId>springbootFirst</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springbootFirst</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</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>
</dependency>
</dependencies>
</project>
- 主加载类
启动框架的入口
package com.winnie.springbootFirst.app;
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);
}
}
- 业务实现
Hello world API
package com.winnie.springbootFirst.app.controller;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@EnableAutoConfiguration
public class TestSpringBootController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!!!!";
}
}
注意:SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描!需合理放置Application类所在位置
项目结构
启动项目
因为spring boot内置了tomcat,所以只需要启动入口实例即可。
右键run as 启动入口
如图所示;浏览器访问localhost:8080 即可- 单元测试
测试重要性不赘述
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.winnie.springbootFirst.controller.TestSpringBootController;
import org.json.JSONObject;
import org.junit.Assert;
@RunWith(SpringRunner.class)
@SpringBootTest(classes=TestSpringBootController.class)
public class ApplicationTest {
private MockMvc mockMvc;
@Autowired
TestSpringBootController testSpringBootController;
@Before
public void setUp() throws Exception{
//通过MockMvcBuilders.xxxSetup().build()创建一个MockMvc进行测试;
mockMvc = MockMvcBuilders.standaloneSetup(testSpringBootController).build();
}
@Test
public void testInfo() throws Exception {
// JSONObject param = new JSONObject() ;
// String jsonstr = param.toString() ;
// System.out.println("================================请求入参:"+jsonstr);
// RequestBuilder request = MockMvcRequestBuilders.post("/")
// .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
// .header("SESSIONNO", "").content(jsonstr) ;
//get
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON).characterEncoding("UTF-8")).andReturn() ;
//post
// MvcResult mvcResult = mockMvc.perform(request).andReturn() ;
int status = mvcResult.getResponse().getStatus();
String content = mvcResult.getResponse().getContentAsString();
Assert.assertTrue("正确", status == 200);
Assert.assertFalse("错误", status != 200);
System.out.println("返回结果:"+status);
System.out.println(content);
}
}