SpringBoot系列:快速入门
环境
- Spring Boot 2.0.4.RELEASE
- JDK 1.8.0_181
- Maven 3.5.4
- IntelliJ IDEA 2018.1.6
创建项目
方式一(推荐)
使用IDEA创建项目
- File ⇒ new ⇒ Project ⇒ Spring initializr
注意
- 确认Project SDK 是否为1.8+
- 点击Next
说明
修改相应的信息
- Group:项目组织唯一标识,eg:com.itxiaoer
- Artifact:项目唯一标识,eg:demo-spring-boot
- Type:项目管理工具类型,可选一般为Maven和Grade,这里使用默认
- Language:java
- Packaging:打包的类型,可选一般为jar和war,一般使用jar
- Java Version:java版本,一般不需要修改
- Version:项目版本号,可自定义
- name:项目名称,可自定义
- Description:项目描述,可自定义
- package:代码初始报名,可自定义
- 如图
- 点击Next
说明
- 注意spring-boot的版本,本demo选择2.0.4
- 其他组件只需要选择web即可,也可以pom.xml自行添加
- 点击Next
说明
- Project Name:项目名称,可修改
- Project Location:项目存放路径,可修改
- 点击Finish 第一个Spring Boot项目创建完成
方式二
- 打开网站
编辑
点击Generate Project后会自动下载
解压下载的zip包
导入到IDEA即可
项目结构
代码结构
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>com.itxiaoer</groupId>
<artifactId>demo-spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo-spring-boot</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
编写请求
package com.itxiaoer.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author : liuyk
*/
@RestController
@SpringBootApplication
public class DemoSpringBootApplication {
@GetMapping("/hello")
public String hello() {
return "Hello world";
}
public static void main(String[] args) {
SpringApplication.run(DemoSpringBootApplication.class, args);
}
}
启动
访问
看启动日志服务端口为:8080,访问地址:http://127.0.0.1:8080/hello
mock测试
package com.itxiaoer.demo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MockServletContext.class)
public class DemoSpringBootApplicationTests {
private MockMvc mock;
@Before
public void before() {
mock = MockMvcBuilders.standaloneSetup(new DemoSpringBootApplication()).build();
}
@Test
public void contextLoads() throws Exception {
mock.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Hello world")));
}
}
完整代码:https://github.com/liuyukuai/demo-spring-cloud/tree/master/demo-spring-boot