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,可参考下图所示:
- 3、点击Generate Project下载项目压缩包并解压。
- 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项目依赖项。
- 5、打开
SpringBootDemo.ipr
即可启动IDEA开发环境。 - 6、配置Gradle
- 7、配置Spring映射
直接点击Create Default即可。
至此,SpringBoot开发环境就搭建完成了。
下面通过一个例子,来快速的开发一个基于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.
测试:
- 1、执行SpringBootDemoApplication.java里的main方法。
- 2、使用Gradle执行
或者在build.gradle目录下执行gradle bootRun
- 3、执行gradle assemble
生成该项目的jar包,使用java -jar
来执行。
测试:
curl http://localhost:8080/test
Hello World!