Spring Boot 构建企业级博客学习(二)

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/qq_35367612/article/details/81215861

第一个hello word 项目

  • 编写项目构建信息
  • 编写程序代码
  • 编写测试用例
  • 配置Gradle Wrapper
  • 运行程序

编写项目构建信息

  1. 修改版本号
    0.0.1-SNAPSHOT 改为  1.0.0
  2. 修改依赖仓库地址,使用阿里云仓库加速构建
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } 
maven{ url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}

编写程序代码

1.编写HelloController

@RestController
public class HelloController {
	
	@RequestMapping("hello")
	public String main() {
		return "hello word";
	}
	
}

2.编写测试用例

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class HelloControllerTests {
	
	@Autowired
	private MockMvc mockMvc;

	@Test
	public void helloTest() throws Exception {
		mockMvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
		.andExpect(status().isOk())
		.andExpect(content().string(equalTo("hello word")));
	}

}

注意:

       添加@AutoConfigureMockMvc注解,否则MockMvc无法注入

配置Gradle Wrapper

修改gradle-wrapper.properties文件

#Tue Feb 06 12:27:20 CET 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9.0-bin.zip

主要修改最后一行,将其修改为需要的版本

然后在项目目录下,就可以使用gradlew build命令进行构建

gradlew build

猜你喜欢

转载自blog.csdn.net/qq_35367612/article/details/81215861