一、简介
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
多年以来,Spring IO平台饱受非议的一点就是大量的XML配置以及复杂的依赖管理。在去年的SpringOne 2GX会议上,Pivotal的CTO Adrian Colyer回应了这些批评,并且特别提到该平台将来的目标之一就是实现免XML配置的开发体验。Boot所实现的功能超出了这个任务的描述,开发人员不仅不再需要编写XML,而且在一些场景中甚至不需要编写繁琐的import语句。在对外公开的beta版本刚刚发布之时,Boot描述了如何使用该框架在140个字符内实现可运行的web应用,从而获得了极大的关注度,该样例发表在tweet上。
Spring Boot不生成代码,且完全不需要XML配置。其主要目标如下:
- 为所有的Spring开发工作提供一个更快、更广泛的入门经验。
- 开箱即用,你也可以通过修改默认值来快速满足你的项目的需求。
- 提供了一系列大型项目中常见的非功能性特性,如嵌入式服务器、安全、指标,健康检测、外部配置等。
Spring Boot官网: http://projects.spring.io/spring-boot/
二、开发环境准备
IDE:IntelliJ IDEA
官网地址:https://www.jetbrains.com/idea/download/
JDK:1.8
Maven
数据库:MySQL
我将以一个用户积分系统为例,开发一个Restful风格的服务端
三、第一个Restful程序
1.新建一个普通Maven工程
创建项目完成后目录结构如下图所示
2.在POM文件中加入对Spring-Boot的依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.bluecoffee</groupId> <artifactId>mapp</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project></div>
3.新建一个RestController来接收客户端的请求,我们来模拟一个登录请求
package com.yepit.mapp.rest; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.*; /** * Created by qianlong on 16/7/20. */ @RestController public class UserController { @RequestMapping(value = "/users/{username}",method = RequestMethod.GET,consumes="application/json") public String getUser(@PathVariable String username, @RequestParam String pwd){ return "Welcome,"+username; }}</div>
- 关键字@RestController代表这个类是用Restful风格来访问的,如果是普通的WEB页面访问跳转时,我们通常会使用@Controller
- value = "/users/{username}" 代表访问的URL是"http://host:PORT/users/实际的用户名"
- method = RequestMethod.GET 代表这个HTTP请求必须是以GET方式访问
- consumes="application/json" 代表数据传输格式是json
- @PathVariable将某个动态参数放到URL请求路径中
- @RequestParam指定了请求参数名称
4.新建启动Restful服务端的启动类
package com.yepit.mapp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Created by qianlong on 16/7/20. */ @SpringBootApplication public class MappRunApplication { public static void main(String[] args) { SpringApplication.run(MappRunApplication.class, args); } }</div>
5.执行MappRunApplication的Main方法启动Restful服务,可以看到控制台有如下输出
. ____ _ __ _ _ /\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\ ( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\ \\\\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.3.3.RELEASE) 2016-07-20 16:49:43.334 INFO 2106 --- [ main] com.yepit.mapp.MappRunApplication : Starting MappRunApplication on bogon with PID 2106 (/Users/qianlong/workspace/spring-boot-samples/target/classes started by qianlong in /Users/qianlong/workspace/spring-boot-samples) 2016-07-20 16:49:43.338 INFO 2106 --- [ main] com.yepit.mapp.MappRunApplication : No active profile set, falling back to default profiles: default 2016-07-20 16:49:43.557 INFO 2106 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@543e710e: startup date [Wed Jul 20 16:49:43 CST 2016]; root of context hierarchy 2016-07-20 16:49:44.127 INFO 2106 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.clas