上篇文章简单介绍了Spring Boot,讲述了Spring Boot的一个理念”习惯优于配置”,以及其操作方便,开箱即用的优点。本篇文章就用一个简单的HelloWorld实例具体讲述一下Spring Boot的上述特性,主要目标是完成Spring Boot基础项目的构建,并且实现一个简单的Http请求处理,通过这个例子对Spring Boot有一个初步的了解,并体验其结构简单、开发快速的特性。
-
项目结构
| .gitignore
| pom.xml
| springboot-01-helloworld.iml
+---src
| +---main
| | +---java
| | | \---com
| | | \---zhuoli
| | | \---service
| | | | SpringBootHelloWorldApplicationContext.java
| | | |
| | | \---controller
| | | HelloWorldController.java
| | |
| | \---resources
| \---test
| \---java
-
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>com.zhuoli.service</groupId>
<artifactId>springboot-01-helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<dependencies>
<!-- Spring Boot web依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
-
Controller层
@RestController
@RequestMapping(value = "/hello")
public class HelloWorldController {
@RequestMapping(value = "/say_hello", method = RequestMethod.GET)
public String sayHello() {
return "Hello,World!";
}
}
@RestController:SpringMVC的注解,相当于@ResponseBody + @Controller两个注解的合体,controller return的数据直接作为数据返回,而不用通过viewResolver解析。如果只使用@Controller注解,controller return的数据会被viewResolver拦截,并解析到特定的页面,如果页面不存在,会报错。
@RequestMapping:SpringMVC的注解,提供路由信息,示例中sayHello的触发路由为”机器名:端口/hello/say_hello”,比如”http://127.0.0.1:8080/hello/say_hello”
-
启动应用类
@SpringBootApplication
public class SpringBootHelloWorldApplicationContext {
public static void main(String[] args) {
SpringApplication.run(SpringBootHelloWorldApplicationContext.class, args);
}
}
@SpringBootApplication是Spring Boot的核心注解,它是一个组合注解,组合了@Configuration、@EnableAutoConfiguration、@ComponentScan。@EnableAutoConfiguration让Spring Boot根据类路径中的jar包依赖为当前项目进行自动配置。比如,添加了spring-boot-start-web依赖,会自动添加Tomcat和Spring MVC的依赖,那么Spring Boot会对Tomcat和Spring MVC进行自动配置(这就是Spring Boot项目未对Tomcat进行任何配置,项目却会使用Tomcat的默认端口8080)。Spring Boot会自动扫描@SpringBootApplication所在类的同级包和下级包里的Bean,所以入口类一般放在groupId + arctifactId组合包下(比如com.zhuoli.service.springboot.helloworld)。
-
Intellij Idea配置 Edit Configurations启动程序
-
点击Run或者Debug启动程序
2018-07-23 11:01:49.264 INFO 2084 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2018-07-23 11:01:49.268 INFO 2084 --- [ main] s.SpringBootHelloWorldApplicationContext : Started SpringBootHelloWorldApplicationContext in 2.21 seconds (JVM running for 3.425)
2018-07-23 11:02:21.552 INFO 2084 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-07-23 11:02:21.552 INFO 2084 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-07-23 11:02:21.578 INFO 2084 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 26 ms
然后访问 http://127.0.0.1:8080/hello/say_hello,就可以看到HelloWorldController return的数据“Hello,World!”
示例代码:码云 – 卓立 – Spring Boot Hello World示例
参考链接:
- Java EE开发的颠覆者 – Spring Boot实战
- Spring官方文档 – Spring IO