Spring Boot 本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速、敏捷地开发新一代基于Spring框架的应用程序。也就是说,它并不是用来替代Spring的解决方案,而是和Spring框架紧密结合用于提升Spring开发者体验的工具,同时它集成了大量常用的第三方库配置(例如Jackson, JDBC, Mongo, Redis, Mail等等),Spring Boot应用中这些第三方库几乎可以零配置的开箱即用(out-of-the-box),大部分的Spring Boot应用都只需要非常少量的配置代码,开发者能够更加专注于业务逻辑
编译器 :idea2017.1.2
完成上面的步骤就已经构建了一个Maven项目了,下面需要创建SpringBoot的配置文件
pom.xml文件:
<?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>Example-SpringBoot-Swagger-Group</groupId> <artifactId>Example-SpringBoot-Swagger</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> </project>
application.properties:
//端口 server.port=8080
html、css 等文件格式:
ExampleApplication.java:
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Created by shuai on 2017/5/21. */ @SpringBootApplication public class ExampleApplication { public static void main(String[] args) { SpringApplication.run(ExampleApplication.class, args); } }
DemoController.java:
package com.example.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * Created by shuai on 2017/5/21. */ @Controller @RequestMapping(value = "/api/demo") public class DemoController { @RequestMapping(value = "/welcome") public String demoReturnSuccess(){ return "welcome"; } }
welcome.html:
<!DOCTYPE html> <html lang="en"> <head> <title>欢迎来到SpringBoot</title> </head> <body> <h1>欢迎来到SpringBoot</h1> </body> </html>
到此简单的SpringBoot项目就已经创建成功了,运行 ExampleApplication.java文件,启动成功后。
在浏览器中输入: http://localhost :8080/api/demo/welcome 就可以看见 templates 文件下的welcome.html文件内容
github地址: Spring Boot 教程、技术栈、示例代码