1.SpringBoot项目构建:http://start-spring.io 自动化构建SpringBoot项目,保存在本地并解压
2.安装gradle并配置gradle环境
3.配置阿里云maven仓库:修改gradle.build 里面额maven仓库,改为:maven{ url 'http://maven.aliyun.com/nexus/content/groups/public' }
4.构建项目,进入解压后的项目路径下,打开控制台执行以下命令:cmd -> cd E:/hello ->E: -> gradle build ->java -jar build/libs/xxxxx.jar
5.导入开发工具,进行测试,测试代码如下:
在main目录下,新建HelloController类:
package com.waylau.spring.boot.blog.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/hello") public String hello(){ return "Hello World!"; } }
在test下,新建HelloControllerTest类:
package com.waylau.spring.boot.blog.contrller; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; 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.request.MockMvcRequestBuilders; 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; @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class HelloControllerTest { @Autowired private MockMvc mockMvc; @Test public void testHello() throws Exception { mockMvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().string(equalTo("Hello World!"))); } }