Spring boot Hello World示例
在本教程中,我们将看到如何使用Thymeleaf创建Spring Boot Hello World示例。
为什么Spring启动?
假设我们想创建Spring MVC MySQL Project.When我们正在处理此项目时,我们可能会与它需要的很多依赖项混淆。
我们可能会与版本混淆。
Spring Boot将解决上面的问题.Pring启动将更快地创建Spring应用程序。
它附带了所有必需的依赖项和嵌入式服务器。
我们没有担心版本不匹配或者兼容性问题。
让我们看看Spring Boot如何在寿命中浮现更简单。
我将使用Spring Boot创建Spring MVC Hello World示例。
Spring boot Hello World:
以下是使用Trymeleaf创建Spring Boot Hello Word示例的步骤。
用于创建以下项目的工具:
- Spring Boot 1.5.3.RELEASE
- Spring 4.3.8.RELEASE
- Thymeleaf 2.5.1 RELEASE
- Tomcat Embed 8
- Maven 3
- Java 8
- Eclipse
第1步:使用名为"springboothellylldexampledhyhyh"的Eclipse中的Maven创建动态Web项目。
maven依赖项:
第2步:更改POM.xml如下所示:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.igi.theitroad</groupId> <artifactId>SpringBootHelloWorldExampleThymeleaf</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringBootHelloWorldExampleThymeleaf Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <finalName>SpringBootHelloWorldExampleThymeleaf</finalName> </build> </project>
Spring-Boot-Starter-Parent为我们提供任何Spring项目所需的所有Maven默认值。
由于我们正在使用百素开发Web应用程序,因此我们还需要添加Spring boot启动器 - thymeleaf依赖。
这将包括其他依赖性此类应用程序所需的Spring boot,thymeleaf,Tomcat等。
如果您注意到,我们没有为特定组件提供任何版本。您只需要为springboot提供版本号(1.5.3.RELEASE)。
第3步:创建名为"org.igi.theitroad.springboot"的包创建一个名为"helloworldcontroller.java"的控制器类
package org.igi.theitroad.springboot; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class HelloWorldController { @RequestMapping("/") public String index() { return "index"; } @RequestMapping("/helloworld") public ModelAndView hello() { String helloWorldMessage = "Hello world from theitroad!"; return new ModelAndView("hello", "message", helloWorldMessage); } }
由于请求首先转到DispatcherServlet并将其重定向到控制器类。
这里@controller描绘了这是我们的控制器类。
@RequestMapper用于将传入的HTTP请求映射到Handler方法(Hello()中的控制器中的Hello()).so hellorldcontroller.java的方法将处理来自Dispatcher.so的Get请求。
当我们有URL时
http://localhost:8080/helloworld or http://localhost:8080/helloworld.html
上面的方法将被称为。
第4步:创建名为"org.igi.theitroad"的包创建一个名为"springboothellyorldapplication.java"的类。
package org.igi.theitroad; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootHelloWorldApplication { public static void main(String[] args) { SpringApplication.run(SpringBootHelloWorldApplication.class, args); } }
我们刚刚添加了@springbootapplication,它做了所有的工作。
让我们了解更多关于这个注释的信息。
@springBootApplication是一个添加以下所有内容的注释:
@configuration使类作为应用程序上下文的Bean定义的源。
@EnableAutoConfiguration允许Spring Boot在ClassPath设置和各种属性设置中添加Bean礼物。
通常,我们将为Spring MVC应用程序添加@bableWebMVC,但Spring Boot会在类路径上看到Spring-WebMVC时自动添加它。
将应用程序标记为Web应用程序,并激活诸如设置DispatcherServlet之类的关键行为。
@ComponentsCan告诉Spring在默认包中查找其他组件,配置和服务,允许它找到控制器。
如果未定义特定包,则会从声明此注释的类的包中扫描。
步骤5:创建名为Application.properties的属性文件,如下所示,并将其放在SRC/Main/Resours中。
spring.thymeleaf.cache: false
以上属性用于禁用模板的缓存。
步骤6:在SRC/Java/Resources文件夹中创建名为"templates"的文件夹,并创建名为"index.html"的文件,如下所示:
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" <title>HelloWorld</title> </head> <body> <a href="helloworld.html">Click here to read hello message </a> </body> </html>
步骤7:在上面创建的Templates文件夹中创建Hello.html
<html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" <title>Hello</title> </head> <body> <p th:text="'${message}'" </body> </html>
这完全是关于Spring的boot子Hello World例子使用百里香。
第8步:这是时候做Maven Build。
右键单击项目 - >运行AS - > Maven Build
步骤9:提供目标作为清洁安装Spring 引导:运行(如下所示)并单击"运行"
我们将在控制台中看到以下输出:
[INFO] Scanning for projects... [INFO] [INFO] ———————————————————————— [INFO] Building SpringBootHelloWorldExampleThymeleaf Maven Webapp 0.0.1-SNAPSHOT [INFO] ———————————————————————— [INFO] [INFO] — maven-clean-plugin:2.6.1:clean (default-clean) @ SpringBootHelloWorldExampleThymeleaf — [INFO] Deleting /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExampleThymeleaf/target [INFO] [INFO] — maven-resources-plugin:2.6:resources (default-resources) @ SpringBootHelloWorldExampleThymeleaf — [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 2 resources [INFO] [INFO] — maven-compiler-plugin:3.1:compile (default-compile) @ SpringBootHelloWorldExampleThymeleaf — [INFO] Changes detected - recompiling the module! [INFO] Compiling 2 source files to /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExampleThymeleaf/target/classes [INFO] [INFO] — maven-resources-plugin:2.6:testResources (default-testResources) @ SpringBootHelloWorldExampleThymeleaf — [INFO] Not copying test resources [INFO] [INFO] — maven-compiler-plugin:3.1:testCompile (default-testCompile) @ SpringBootHelloWorldExampleThymeleaf — [INFO] Not compiling test sources [INFO] [INFO] — maven-surefire-plugin:2.18.1:test (default-test) @ SpringBootHelloWorldExampleThymeleaf — [INFO] Tests are skipped. [INFO] [INFO] — maven-war-plugin:2.6:war (default-war) @ SpringBootHelloWorldExampleThymeleaf — [INFO] Packaging webapp [INFO] Assembling webapp [SpringBootHelloWorldExampleThymeleaf] in [/Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExampleThymeleaf/target/SpringBootHelloWorldExampleThymeleaf] [INFO] Processing war project [INFO] Copying webapp resources [/Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExampleThymeleaf/src/main/webapp] [INFO] Webapp assembled in [375 msecs] [INFO] Building war: /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExampleThymeleaf/target/SpringBootHelloWorldExampleThymeleaf.war [INFO] [INFO] — maven-install-plugin:2.5.2:install (default-install) @ SpringBootHelloWorldExampleThymeleaf — [INFO] Installing /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExampleThymeleaf/target/SpringBootHelloWorldExampleThymeleaf.war to /Users/apple/.m2/repository/org/igi/theitroad/SpringBootHelloWorldExampleThymeleaf/0.0.1-SNAPSHOT/SpringBootHelloWorldExampleThymeleaf-0.0.1-SNAPSHOT.war [INFO] Installing /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExampleThymeleaf/pom.xml to /Users/apple/.m2/repository/org/igi/theitroad/SpringBootHelloWorldExampleThymeleaf/0.0.1-SNAPSHOT/SpringBootHelloWorldExampleThymeleaf-0.0.1-SNAPSHOT.pom [INFO] [INFO] >>> spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) > test-compile @ SpringBootHelloWorldExampleThymeleaf >>> [INFO] [INFO] — maven-resources-plugin:2.6:resources (default-resources) @ SpringBootHelloWorldExampleThymeleaf — [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 2 resources [INFO] [INFO] — maven-compiler-plugin:3.1:compile (default-compile) @ SpringBootHelloWorldExampleThymeleaf — [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] — maven-resources-plugin:2.6:testResources (default-testResources) @ SpringBootHelloWorldExampleThymeleaf — [INFO] Not copying test resources [INFO] [INFO] — maven-compiler-plugin:3.1:testCompile (default-testCompile) @ SpringBootHelloWorldExampleThymeleaf — [INFO] Not compiling test sources [INFO] [INFO] <<< spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) < test-compile @ SpringBootHelloWorldExampleThymeleaf <<< [INFO] [INFO] — spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) @ SpringBootHelloWorldExampleThymeleaf —. ____ _ __ _ _ /\/___'_ __ _ _(_)_ __ __ _ ( ( )___ | '_ | '_| | '_/_` | \/___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |___, |//// =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.3.RELEASE)2016-04-28 16:40:05.395 INFO 1967 — [ main] o.a.j.SpringBootHelloWorldApplication : Starting SpringBootHelloWorldApplication on apples-MacBook-Air.local with PID 1967 (/Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExampleThymeleaf/target/classes started by apple in /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExampleThymeleaf) 2016-04-28 16:40:05.403 INFO 1967 — [ main] o.a.j.SpringBootHelloWorldApplication : No active profile set, falling back to default profiles: default 2016-04-28 16:40:06.087 INFO 1967 — [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@330dfb29: startup date [Fri Apr 28 16:40:06 IST 2016]; root of context hierarchy 2016-04-28 16:40:07.740 INFO 1967 — [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2016-04-28 16:40:07.756 INFO 1967 — [ main] o.apache.catalina.core.StandardService : Starting service Tomcat 2016-04-28 16:40:07.757 INFO 1967 — [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.14 2016-04-28 16:40:07.926 INFO 1967 — [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2016-04-28 16:40:07.927 INFO 1967 — [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1840 ms 2016-04-28 16:40:08.140 INFO 1967 — [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2016-04-28 16:40:08.145 INFO 1967 — [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2016-04-28 16:40:08.145 INFO 1967 — [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2016-04-28 16:40:08.146 INFO 1967 — [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2016-04-28 16:40:08.146 INFO 1967 — [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2016-04-28 16:40:08.564 INFO 1967 — [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@330dfb29: startup date [Fri Apr 28 16:40:06 IST 2016]; root of context hierarchy 2016-04-28 16:40:08.656 INFO 1967 — [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped “{[/]}" onto public java.lang.String org.igi.theitroad.springboot.HelloWorldController.index() 2016-04-28 16:40:08.658 INFO 1967 — [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped “{[/helloworld]}" onto public org.springframework.web.servlet.ModelAndView org.igi.theitroad.springboot.HelloWorldController.hello() 2016-04-28 16:40:08.662 INFO 1967 — [ 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) 2016-04-28 16:40:08.663 INFO 1967 — [ 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) 2016-04-28 16:40:08.707 INFO 1967 — [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016-04-28 16:40:08.707 INFO 1967 — [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016-04-28 16:40:08.762 INFO 1967 — [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016-04-28 16:40:09.402 INFO 1967 — [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2016-04-28 16:40:09.496 INFO 1967 — [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2016-04-28 16:40:09.502 INFO 1967 — [ main] o.a.j.SpringBootHelloWorldApplication : Started SpringBootHelloWorldApplication in 4.768 seconds (JVM running for 14.268)
步骤10:让我们现在测试应用程序。
随着Spring使用嵌入式Tomcat,我们可以使用http://localhost:8080 /。