SpringBoot HelloWorld

SpringBoot简介

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

使用Gradle构建项目

  • 1、访问 http://start.spring.io/
  • 2、选择构建工具Gradle Project、Spring Boot版本选择默认1.5.4以及填写一些工程基本信息,点击“Switch to the full version.”java版本默认选择1.8,可参考下图所示:
    Spring.io
  • 3、点击Generate Project下载项目压缩包并解压。
    archieve
  • 4、打开build.gradle做如下配置:
buildscript {
    ext {
        springBootVersion = '1.5.4.RELEASE'
    }
    repositories {
        maven {
            url 'http://maven.aliyun.com/nexus/content/groups/public/'
        }
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
    maven {
        url 'http://maven.aliyun.com/nexus/content/groups/public/'
    }
    mavenCentral()
}
dependencies {
    compile('org.springframework.boot:spring-boot-starter')
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

apply plugin: 'idea' :生成IntelliJ IDEA开发环境(cmd下执行 gradle idea)。
compile('org.springframework.boot:spring-boot-starter-web') :开发web项目依赖项。
idea

  • 5、打开SpringBootDemo.ipr即可启动IDEA开发环境。
  • 6、配置Gradle
    Gradle
  • 7、配置Spring映射
    Spring Mapping
    直接点击Create Default即可。

至此,SpringBoot开发环境就搭建完成了。
architecture

下面通过一个例子,来快速的开发一个基于Spring Boot的Rest应用。

1、在cn.bjut.SpringBootDemo目录下建立controller文件夹,编写一个简单的controller。

package cn.bjut.SpringBootDemo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * Created by N3verL4nd on 2017/7/11.
 */
@RestController
public class TestController {
    @RequestMapping("/test")
    public String test() {
        return "Hello World!";
    }
}

@RestController 解释:

@RestController is a stereotype annotation that combines @ResponseBody and @Controller.
all
测试:
- 1、执行SpringBootDemoApplication.java里的main方法。
- 2、使用Gradle执行
bootRun
或者在build.gradle目录下执行gradle bootRun
- 3、执行gradle assemble 生成该项目的jar包,使用java -jar 来执行。
gradle assemble

测试:

curl http://localhost:8080/test
Hello World!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

N3verL4nd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值