SpringBoot + gradle 项目搭建

2017-08-02  本文已影响0人  魔焰joy

工具:

IDEA , gradle

步骤:

buildscript {
    repositories {
        mavenCentral()//依赖Maven仓库
    }
    dependencies {
        //使用1.4.2.RELEASE版本的Spring框架
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.2.RELEASE")
    }
}

group 'com.jktaihe'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'spring-boot'

sourceCompatibility = 1.8
targetCompatibility = 1.8

//repositories {
//    mavenCentral()
//}

jar {
    baseName = 'gs-gradle'
    version =  '0.1.0'
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

package com.jktaihe.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by hzjixiaohui on 2017-8-2.
 */

//表明这是一个 Controller
@Controller
//RestController是一种Rest风格的Controller,可以直接返回对象而不返回视图,返回的对象可以使JSON,XML等
//@RestController

//使用自动配置,主动添加并解析bean,配置文件等信息
@EnableAutoConfiguration
public class SpringController {
    //设置访问的url
    @RequestMapping("/")
    //表示返回JSON格式的结果,如果前面使用的是@RestController可以不用写
    @ResponseBody
    String home() {
        return "Hello World!";//返回结果为字符串
    }
    public static void main(String[] args) throws Exception {
        //通过SpringApplication的run()方法启动应用,无需额外的配置其他的文件
        SpringApplication.run(SpringController.class, args);
    }
}


参考

https://spring.io/guides/gs/rest-service/

上一篇 下一篇

猜你喜欢

热点阅读