SpringBoot第一个可运行实例
包含内容: 源码,全套工具
作者QQ549710689
本实例向SpringBoot的学习者提供了第一个可运行实例, 通过本实例的学习,你将了解一个SpringBoot的框架设计, 配置,以及如何运行SpringBoot工程(无须中间件Tomcat, JBoss), SpringBoot降低了学习成本,但配备了更强大的功能
项目对应的实例代码可以通过顶部【下载实例】按钮获取
开发工具: Eclipse Mars, JDK1.7, Maven3.0.2
【项目包含内容】(见下图):
导入工程后见下图
1. 开发springboot必须要用maven插件管理项目
2. 配置pom.xml 所需要jar包
<?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.example</groupId> <artifactId>springBoot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springBoot</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <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>
3. 配置主应用程序入口, 只要启动这个主类便可以启动工程
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
4. 编写相应的业务接口
@Controller @EnableAutoConfiguration public class HelloWorldController { @RequestMapping("/hello") @ResponseBody public String index() { return "Hello World"; } }
5. 启动应用程序, 默认监听在端口8080上
6. 调用接口, 返回相应的数据