环境:
jdk1.8;spring boot2.0.2;Maven3.3
步骤:
1.生成标准项目
通过SPRING INITIALIZR
工具产生基础项目;
网址为http://start.spring.io/点击打开链接
此次选择Maven构建项目,版本选择2.0.2;初始模块只选择添加web,可同时添加多个;生成下载解压;
2.项目导入及结构
右击--》import--》Maven--》Existing Mevan Projects 选择刚刚解压的项目即可
项目结构为:
src/main/java
下的程序入口:DemoApplication
src/main/resources
下的配置文件:application.properties
src/test/
下的测试入口:DemoApplicationTests
生成的程序和测试类可立即使用
初始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.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<!-- 引入并指定spring-boot父版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<!-- 指定编码格式及JDK版本 -->
<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>
<!-- Web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 测试模块,包括JUnit、Hamcrest、Mockito -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 引入spring-boot插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.编写HelloWorld及服务基本配置
编写HelloWorld如下
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//Spring4之后加入的注解,原来在@Controller中返回json需要@ResponseBody来配合,
//如果直接用@RestController替代@Controller就不需要再配置@ResponseBody,默认返回json格式。
@RestController
public class HelloController {
// Spring mvc的注解,起到路由作用
@RequestMapping("/hello")
public Map<String, Object> index() {
Map<String, Object> test = new HashMap<String, Object>();
test.put("path", "/hello");
test.put("date", new Date());
return test;
}
}
可在application.properties配置服务端口及上下文路径,这里要注意下2.0和1.0的配置项的不同
server.port=9999
#注意此处2.0使用的是server.servlet.context-path,1.0使用的是server.context-path
server.servlet.context-path=/demo
在DemoApplication类下使用main方法启动项目
访问http://127.0.0.1:9999/demo/hello;返回如下:
{"date":"2018-05-25T06:45:20.282+0000","path":"/hello"}
4.测试类的使用
编写一个全栈测试类:
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.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@RunWith(SpringRunner.class)
// 引入SpringBootTest并生成随机接口
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class DemoApplicationTests {
// 注入随机接口
@LocalServerPort
private int port;
// 引入测试模版类
@Autowired
private TestRestTemplate restTemplate;
@Test
public void testHello() {
System.out.println(port);
// 构建参数
MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
// 使用restTemplate的exchange方法进行调用
ResponseEntity<String> entity = this.restTemplate.exchange("/hello", HttpMethod.POST,
new HttpEntity<>(form, null), String.class);
System.out.println(entity.getStatusCode());
System.out.println(entity.getBody());
}
}
多次运行你可以看到打印的端口的不同!