欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

史上最简单的SpringBoot教程之如何打印HelloWorld

程序员文章站 2022-04-22 13:38:52
...

一、前言

Spring boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。
Spring boot包含的特性:

  • 创建独立的Spring应用程序
  • 嵌入Tomcat,无需部署war文件
  • 简化Maven配置
  • 自动配置Spring
  • 提供生产就绪功能,如指标,健康检查和外部配置
  • 开箱即用,没有代码生成,也无需XML配置

为基于Spring的开发提供更快的入门体验,开箱即用,没有代码生成,也无需XML配置。同时也可以修改默认值来满足特定需求。提供了一些大型项目中常见的非功能特性,如嵌入式服务器、安全、指标、健康体检、外部配置等。Spring boot并不是对Spring功能的增强,而是提供了一种快速使用Spring的方式。

二、准备

开发环境:1.7.0_79
开发工具:Eclipse
项目管理工具:Maven3
操作系统:Windows8

Spring boot版本:1.4.1.RELEASE

三、案例

搭建一个Maven工程SpringBootHelloWorld

史上最简单的SpringBoot教程之如何打印HelloWorld

POM文件中导入相关依赖

<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.queen.springboot</groupId>
	<artifactId>SpringBootHelloWorld</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.1.RELEASE</version>
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.7</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
</project>
我们引入了Spring boot的父节点依赖,引入这个之后相关的引用就不需要添加version配置了,Spring boot会自动选择最合适的版本进行添加。
环境中我们指定JDK的版本为1.7,默认是1.6

导入spring-boot-starter-web依赖这个地方我们没有指定版本号,是因为上面我们配置了parent,所以不用添加了。

编写控制类SpringBootHelloWorld

@RestController
public class HelloWorldController {
	
	@RequestMapping("/hello")
	public String sayHello() {
		return "helloWorld";
	}
}

这里我们使用的@RestController注解等价于@Controller和@ResponseBody注解,使用@RequestMapping建立请求映射。

编写启动类APP

@SpringBootApplication
public class APP {

	public static void main(String[] args) {
		SpringApplication.run(APP.class, args);
	}

}

使用@SpringBootApplication注解定义APP是一个SpringBoot类型的应用,在main方法中使用SpringApplication的run方法启动我们的程序。

右键Run As -> Java Application,启动应用,控制台启动日志如下:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.1.RELEASE)

2017-07-16 10:31:07.706  INFO 12108 --- [           main] com.queen.controller.APP                 : Starting APP on Lenovo-PC with PID 12108 (E:\queen\SpringBootHelloWorld\target\classes started by think in E:\queen\SpringBootHelloWorld)
2017-07-16 10:31:07.709  INFO 12108 --- [           main] com.queen.controller.APP                 : No active profile set, falling back to default profiles: default
2017-07-16 10:31:07.814  INFO 12108 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot[email protected]6568614: startup date [Sun Jul 16 10:31:07 CST 2017]; root of context hierarchy
2017-07-16 10:31:10.086  INFO 12108 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-07-16 10:31:10.099  INFO 12108 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2017-07-16 10:31:10.101  INFO 12108 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.5
2017-07-16 10:31:10.201  INFO 12108 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-07-16 10:31:10.202  INFO 12108 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2392 ms
2017-07-16 10:31:10.411  INFO 12108 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-07-16 10:31:10.415  INFO 12108 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-07-16 10:31:10.417  INFO 12108 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-07-16 10:31:10.417  INFO 12108 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-07-16 10:31:10.417  INFO 12108 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-07-16 10:31:10.812  INFO 12108 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot[email protected]6568614: startup date [Sun Jul 16 10:31:07 CST 2017]; root of context hierarchy
2017-07-16 10:31:10.906  INFO 12108 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.queen.controller.HelloWorldController.sayHello()
2017-07-16 10:31:10.913  INFO 12108 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-07-16 10:31:10.913  INFO 12108 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-07-16 10:31:10.965  INFO 12108 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-16 10:31:10.966  INFO 12108 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-16 10:31:11.013  INFO 12108 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-16 10:31:11.320  INFO 12108 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-07-16 10:31:11.391  INFO 12108 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-07-16 10:31:11.398  INFO 12108 --- [           main] com.queen.controller.APP                 : Started APP in 4.301 seconds (JVM running for 4.619)
启动后默认端口号是8080

♦使用http://localhost:8080/hello看是否能够访问我们的应用

史上最简单的SpringBoot教程之如何打印HelloWorld

访问成功。

至此,我们使用Spring boot搭建的第一个HelloWorld小程序完成。


=======欢迎大家拍砖,小手一抖,多多点赞哟!=======

版权声明:本文为博主原创文章,允许转载,但转载必须标明出处。