spring boot入门篇,helloworld案例演示

为什么用spring boot?

  嵌入的 Tomcat,无需部署 WAR 文件

  简化 Maven 配置

  无需 XML 配置,轻松快速地搭建Spring Web应用

开始学习SpringBoot

构建简单spring boot 项目

这里官网提供的生成器SPRING INITIALIZR 来创建简单的spring boot 项目。

1.、访问 http://start.spring.io

2、解压下载的压缩包文件demo.zip至本地目录,打开IDEA ,选择菜单>>File >>Open...

导入成功后,目录结构如下图

3、修改DemoApplication类,如下

 1 package com.example.demo;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RestController;
 7 
 8 @SpringBootApplication
 9 @RestController
10 public class DemoApplication {
11     @RequestMapping("/")
12     public String hello() {
13         return "Hello World";
14     }
15 
16     public static void main(String[] args) {
17         SpringApplication.run(DemoApplication.class, args);
18     }
19 }
View Code

注:@RestController 这个注解相当于同时添加@Controller和@ResponseBody注解。

4、运行DemoApplication.java 启动项目,启动日志可以看到端口为8080

E:\ProgramFiles\java\jdk1.8.0_152\bin\java -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -javaagent:E:\ProgramFiles\IntelliJIDEA\lib\idea_rt.jar=58488:E:\ProgramFiles\IntelliJIDEA\bin -Dfile.encoding=UTF-8 -classpath E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\charsets.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\deploy.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\access-bridge-64.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\cldrdata.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\dnsns.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\jaccess.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\jfxrt.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\localedata.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\nashorn.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\sunec.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\sunjce_provider.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\sunmscapi.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\sunpkcs11.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\ext\zipfs.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\javaws.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\jce.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\jfr.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\jfxswt.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\jsse.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\management-agent.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\plugin.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\resources.jar;E:\ProgramFiles\java\jdk1.8.0_152\jre\lib\rt.jar;D:\Documents\学习素材\spring\demo\target\classes;C:\Users\Arvin\.m2\repo\org\springframework\boot\spring-boot-starter-web\2.0.2.RELEASE\spring-boot-starter-web-2.0.2.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\springframework\boot\spring-boot-starter\2.0.2.RELEASE\spring-boot-starter-2.0.2.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\springframework\boot\spring-boot\2.0.2.RELEASE\spring-boot-2.0.2.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\springframework\boot\spring-boot-autoconfigure\2.0.2.RELEASE\spring-boot-autoconfigure-2.0.2.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\springframework\boot\spring-boot-starter-logging\2.0.2.RELEASE\spring-boot-starter-logging-2.0.2.RELEASE.jar;C:\Users\Arvin\.m2\repo\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;C:\Users\Arvin\.m2\repo\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\Users\Arvin\.m2\repo\org\apache\logging\log4j\log4j-to-slf4j\2.10.0\log4j-to-slf4j-2.10.0.jar;C:\Users\Arvin\.m2\repo\org\apache\logging\log4j\log4j-api\2.10.0\log4j-api-2.10.0.jar;C:\Users\Arvin\.m2\repo\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;C:\Users\Arvin\.m2\repo\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;C:\Users\Arvin\.m2\repo\org\yaml\snakeyaml\1.19\snakeyaml-1.19.jar;C:\Users\Arvin\.m2\repo\org\springframework\boot\spring-boot-starter-json\2.0.2.RELEASE\spring-boot-starter-json-2.0.2.RELEASE.jar;C:\Users\Arvin\.m2\repo\com\fasterxml\jackson\core\jackson-databind\2.9.5\jackson-databind-2.9.5.jar;C:\Users\Arvin\.m2\repo\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;C:\Users\Arvin\.m2\repo\com\fasterxml\jackson\core\jackson-core\2.9.5\jackson-core-2.9.5.jar;C:\Users\Arvin\.m2\repo\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.5\jackson-datatype-jdk8-2.9.5.jar;C:\Users\Arvin\.m2\repo\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.5\jackson-datatype-jsr310-2.9.5.jar;C:\Users\Arvin\.m2\repo\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.5\jackson-module-parameter-names-2.9.5.jar;C:\Users\Arvin\.m2\repo\org\springframework\boot\spring-boot-starter-tomcat\2.0.2.RELEASE\spring-boot-starter-tomcat-2.0.2.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\apache\tomcat\embed\tomcat-embed-core\8.5.31\tomcat-embed-core-8.5.31.jar;C:\Users\Arvin\.m2\repo\org\apache\tomcat\embed\tomcat-embed-el\8.5.31\tomcat-embed-el-8.5.31.jar;C:\Users\Arvin\.m2\repo\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.31\tomcat-embed-websocket-8.5.31.jar;C:\Users\Arvin\.m2\repo\org\hibernate\validator\hibernate-validator\6.0.9.Final\hibernate-validator-6.0.9.Final.jar;C:\Users\Arvin\.m2\repo\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;C:\Users\Arvin\.m2\repo\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;C:\Users\Arvin\.m2\repo\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;C:\Users\Arvin\.m2\repo\org\springframework\spring-web\5.0.6.RELEASE\spring-web-5.0.6.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\springframework\spring-beans\5.0.6.RELEASE\spring-beans-5.0.6.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\springframework\spring-webmvc\5.0.6.RELEASE\spring-webmvc-5.0.6.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\springframework\spring-aop\5.0.6.RELEASE\spring-aop-5.0.6.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\springframework\spring-context\5.0.6.RELEASE\spring-context-5.0.6.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\springframework\spring-expression\5.0.6.RELEASE\spring-expression-5.0.6.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;C:\Users\Arvin\.m2\repo\org\springframework\spring-core\5.0.6.RELEASE\spring-core-5.0.6.RELEASE.jar;C:\Users\Arvin\.m2\repo\org\springframework\spring-jcl\5.0.6.RELEASE\spring-jcl-5.0.6.RELEASE.jar com.example.demo.DemoApplication

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

2018-05-13 12:56:28.095  INFO 15404 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on FX50J4200 with PID 15404 (started by Arvin in D:\Documents\学习素材\spring\demo)
2018-05-13 12:56:28.135  INFO 15404 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2018-05-13 12:56:28.377  INFO 15404 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@448ff1a8: startup date [Sun May 13 12:56:28 CST 2018]; root of context hierarchy
2018-05-13 12:56:31.278  INFO 15404 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-05-13 12:56:31.318  INFO 15404 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-05-13 12:56:31.318  INFO 15404 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.31
2018-05-13 12:56:31.328  INFO 15404 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [E:\ProgramFiles\java\jdk1.8.0_152\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;E:\ProgramFiles\java\jdk1.8.0_152\bin;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;E:\ProgramFiles\MySQL\MysqlServer5.7.14\bin;E:\ProgramFiles\MinGW\mingw32\bin;E:\ProgramFiles\Tools\apache-ant-1.10.1\bin;E:\ProgramFiles\Tools\apache-maven-3.5.0\bin;E:\ProgramFiles\nodejs\;E:\ProgramFiles\Tools\VisualSVN\bin;E:\ProgramFiles\Tools\gradle3.5\bin;E:\ProgramFiles\Tools\Git\Git\cmd;E:\ProgramFiles\Tools\Lua\5.1;E:\ProgramFiles\Tools\Lua\5.1\clibs;E:\ProgramFiles\Tools\Sencha\Cmd;E:\ProgramFiles\Ruby200-x64\bin;C:\Program Files\TortoiseSVN\bin;C:\Program Files\TortoiseGit\bin;E:\ProgramFiles\Tools\Brackets\command;E:\ProgramFiles\python3.6.3;E:\ProgramFiles\python3.6.3\Scripts;%ANDROID_HOME%\tools;E:\ProgramFiles\groovy\groovy-2.4.10\bin;E:\ProgramFiles\android\android-sdk-windows\platform-tools;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;E:\ProgramFiles\OracleClient\instantclient_12_2;E:\ProgramFiles\Tools\UE;C:\Users\Arvin\AppData\Local\Microsoft\WindowsApps;C:\Users\Arvin\AppData\Roaming\npm;E:\ProgramFiles\Tools\SSH Secure Shell;;.]
2018-05-13 12:56:31.462  INFO 15404 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-05-13 12:56:31.462  INFO 15404 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3143 ms
2018-05-13 12:56:31.667  INFO 15404 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-05-13 12:56:31.671  INFO 15404 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-05-13 12:56:31.672  INFO 15404 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-05-13 12:56:31.672  INFO 15404 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-05-13 12:56:31.672  INFO 15404 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-05-13 12:56:31.925  INFO 15404 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-13 12:56:32.235  INFO 15404 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@448ff1a8: startup date [Sun May 13 12:56:28 CST 2018]; root of context hierarchy
2018-05-13 12:56:32.345  INFO 15404 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/index]}" onto public java.lang.String com.example.demo.DemoApplication.index()
2018-05-13 12:56:32.345  INFO 15404 --- [           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.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-05-13 12:56:32.345  INFO 15404 --- [           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.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-05-13 12:56:32.365  INFO 15404 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-13 12:56:32.365  INFO 15404 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-05-13 12:56:32.607  INFO 15404 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-05-13 12:56:32.728  INFO 15404 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-05-13 12:56:32.732  INFO 15404 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 5.951 seconds (JVM running for 8.268)
2018-05-13 12:56:48.617  INFO 15404 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-05-13 12:56:48.617  INFO 15404 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-05-13 12:56:48.633  INFO 15404 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 16 ms
View Code

5、打开浏览器访问:http://localhost:8080可得结果

 

补充

上面讲解了可以从 http://start.spring.io 中直接构建spring-boot项目

下面讲解直接在IntelliJ IDEA中构建spring-boot项目

1、打开 IDEA ,新建项目

2、选择Spring Initializr 并在 Choose Initializr Service URL 填入 https://start.spring.io ,点击Next

3.、填入Group Artifact 等相关信息,点击Next

4、选择你需要依赖的模块,点击Next

5.、点击Finish即可完成项目的创建

posted @ 2018-05-12 21:46  Sarvin  阅读(290)  评论(0编辑  收藏  举报