SpringBoot 是一个新起的项目,目的是让 SpringFramework 更易用。
官网:http://projects.spring.io/spring-boot/
这里用它来快速构建一个 Restful 的 WebService。
创建一个 Maven 项目,在 pom.xml 文件中配置依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
一共 7 个:
1. spring-boot 的核心
2. spring-boot 内嵌了 tomcat,运行项目甚至不需要 tomcat
3. json 和 对象之间的转换工具,很实用
4. 数据验证工具,前台验证如果完善话,其实没什么必要
5. 剩下的是 spring
目录结构:
代码:
public class Person {
private String name;
private Integer age;
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public static Person generatePerson(String name, Integer age) {
Person person = new Person();
person.name = name;
person.age = age;
return person;
}
}
@RestController
public class PersonController {
@RequestMapping("/person")
Person getPerson() {
return Person.generatePerson("Mike", 24);
}
}
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
运行主函数就能启动内嵌的 tomcat 了。
访问结果: