IntelliJ  Gradle构建Spring Boot应用

源码下载:https://download.csdn.net/download/salarzl/10812973

1、IntelliJ 创建Gradle java项目

IntelliJ  Gradle构建Spring Boot应用

2、修改build.gradle文件内容如下:

//应用插件
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'org.unbroken-dome.test-sets'
apply plugin: 'war'
//jdk版本
sourceCompatibility = 1.8
targetCompatibility = 1.8
//构建脚本
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath(
                'org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE',
                'org.unbroken-dome.gradle-plugins:gradle-testsets-plugin:1.0.2'
        )
    }
}
//使用maven仓库
repositories {
    mavenCentral()
}
//具体依赖
dependencies {
    compile(
            'org.springframework.boot:spring-boot-devtools',
            'org.springframework.boot:spring-boot-starter-actuator',
            'org.springframework.boot:spring-boot-starter-web'
    )
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

3、main函数启动SpringBoot应用

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @SpringBootApplication 注解可以让SpringBoot为我们按照默认约定进行所有的应用初始化配置,
 * 本类是在项目中唯一一个包含main方法的类,通常我们将该类创建在项目包的根目录下,
 * 在测试过程中,我们可以通过运行此类来运行SpringBoot应用
 */
@SpringBootApplication
public class SpringBootMain {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootMain.class, args);
    }
}

4、controller定义rest接口

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping(value = "/word",method = RequestMethod.GET)
    public String testHello() {
        return "helloWord";
    }

}

5、gradle build

IntelliJ  Gradle构建Spring Boot应用

6、run

IntelliJ  Gradle构建Spring Boot应用

IntelliJ  Gradle构建Spring Boot应用

7、访问 rest 接口

IntelliJ  Gradle构建Spring Boot应用