Gradle创建springboot项目

来源:互联网 发布:正在激活windows 错误 编辑:程序博客网 时间:2024/06/05 17:51

使用Gradle创建一个最简单的Spring Boot项目及测试
idea软件Gradle构建项目
在下载好的项目里,用gradle build编译项目,再执行build下的libs的jar文件:java -jar build下的libs的jar。
修改 build.gradle 文件

group 'com.jpq'version '1.0-SNAPSHOT'apply plugin: 'java'apply plugin: 'spring-boot'sourceCompatibility = 1.8targetCompatibility = 1.8buildscript {    ext {        springBootVersion = '1.5.6.RELEASE'    }    repositories {        mavenCentral()    }    dependencies {        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")    }}repositories {    mavenCentral()    maven            {                url "http://maven.aliyun.com/nexus/content/groups/public/"            }}dependencies {    compile('org.springframework.boot:spring-boot-starter-web')    testCompile('org.springframework.boot:spring-boot-starter-test')}

在controller层新建hellocontroller类:

@RestControllerpublic class HelloController {    @RequestMapping("/hello")    public String sdd(){        return  "Hello,world";    }}

新建application类

@SpringBootApplicationpublic class Application {     public static void main(String[] args){         SpringApplication.run(Application.class,args);     }}

新建测试类test:(注意最后三项的导入)

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;/** * 校验helloController的/hello */@RunWith(SpringRunner.class)@SpringBootTest@AutoConfigureMockMvcpublic class HelloControllerTest {    @Autowired    private MockMvc mockmvc;    @Test    public void contextLoads() throws Exception {        mockmvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))                .andExpect(status().isOk())                .andExpect(content().string(equalTo("Hello,world")));    }}
原创粉丝点击