spring boot gradle 学习笔记(1)

本文介绍如何使用Spring Boot搭建一个简单的Web应用,包括环境配置、项目创建、添加控制器及运行测试等步骤。

官网quick-start系列:https://projects.spring.io/spring-boot/#quick-start

1、编码环境:jdk版本1.8、intellij idea版本2016.2.5、gradle版本3.2

2、新建项目
项目的build.gralde配置如下:

group 'org.changs'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
}

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

3、添加Application入口函数

package org.changs.learn;

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;

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}

4、添加示例接口:

package org.changs.learn.web;

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

/**
 * Created by yincs on 2017/1/9.
 */
@RestController
public class HelloController {

    @RequestMapping("hello")
    public String hello() {
        return "hello spring-boot";
    }
}

5、运行Application类的main函数,启动成功后用浏览器访问http://localhost:8080/hello。页面上出现hello spring-boot!
ok!初步搭建已完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值