首先创建一个Spring Initializr的项目
给Group和Artifact起名,HelloWorld项目就直接默认即可。
选择Web —> Spring Web
给Project起名称,默认就好,接着Finish
接下来Import Changes,项目的结构如下图所示:
接着在建立一个新的控制器文件HelloController.java
在该文件中添加代码
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping(value = "/hello")
public String hello()
{
return "Hello SpringBoot";
}
}
运行DemoApplication
"C:Program FilesJavajdk1.8.0_161injava.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:G:IDEAmainIntelliJ IDEA 2019.1.1libidea_rt.jar=57145:G:IDEAmainIntelliJ IDEA 2019.1.1in" -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.8.0_161jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_161jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_161jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_161jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_161jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_161jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_161jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_161jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_161jrelibext ashorn.jar;C:Program FilesJavajdk1.8.0_161jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_161jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_161jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_161jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_161jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_161jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_161jrelibjce.jar;C:Program FilesJavajdk1.8.0_161jrelibjfr.jar;C:Program FilesJavajdk1.8.0_161jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_161jrelibjsse.jar;C:Program FilesJavajdk1.8.0_161jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_161jrelibplugin.jar;C:Program FilesJavajdk1.8.0_161jrelib esources.jar;C:Program FilesJavajdk1.8.0_161jrelib t.jar;G:IDEAdemo argetclasses;F:mavenRepositoryorgspringframeworkootspring-boot-starter-web2.1.9.RELEASEspring-boot-starter-web-2.1.9.RELEASE.jar;F:mavenRepositoryorgspringframeworkootspring-boot-starter2.1.9.RELEASEspring-boot-starter-2.1.9.RELEASE.jar;F:mavenRepositoryorgspringframeworkootspring-boot2.1.9.RELEASEspring-boot-2.1.9.RELEASE.jar;F:mavenRepositoryorgspringframeworkootspring-boot-autoconfigure2.1.9.RELEASEspring-boot-autoconfigure-2.1.9.RELEASE.jar;F:mavenRepositoryorgspringframeworkootspring-boot-starter-logging2.1.9.RELEASEspring-boot-starter-logging-2.1.9.RELEASE.jar;F:mavenRepositorychqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar;F:mavenRepositorychqoslogbacklogback-core1.2.3logback-core-1.2.3.jar;F:mavenRepositoryorgapachelogginglog4jlog4j-to-slf4j2.11.2log4j-to-slf4j-2.11.2.jar;F:mavenRepositoryorgapachelogginglog4jlog4j-api2.11.2log4j-api-2.11.2.jar;F:mavenRepositoryorgslf4jjul-to-slf4j1.7.28jul-to-slf4j-1.7.28.jar;F:mavenRepositoryjavaxannotationjavax.annotation-api1.3.2javax.annotation-api-1.3.2.jar;F:mavenRepositoryorgyamlsnakeyaml1.23snakeyaml-1.23.jar;F:mavenRepositoryorgspringframeworkootspring-boot-starter-json2.1.9.RELEASEspring-boot-starter-json-2.1.9.RELEASE.jar;F:mavenRepositorycomfasterxmljacksoncorejackson-databind2.9.9.3jackson-databind-2.9.9.3.jar;F:mavenRepositorycomfasterxmljacksoncorejackson-annotations2.9.0jackson-annotations-2.9.0.jar;F:mavenRepositorycomfasterxmljacksoncorejackson-core2.9.9jackson-core-2.9.9.jar;F:mavenRepositorycomfasterxmljacksondatatypejackson-datatype-jdk82.9.9jackson-datatype-jdk8-2.9.9.jar;F:mavenRepositorycomfasterxmljacksondatatypejackson-datatype-jsr3102.9.9jackson-datatype-jsr310-2.9.9.jar;F:mavenRepositorycomfasterxmljacksonmodulejackson-module-parameter-names2.9.9jackson-module-parameter-names-2.9.9.jar;F:mavenRepositoryorgspringframeworkootspring-boot-starter-tomcat2.1.9.RELEASEspring-boot-starter-tomcat-2.1.9.RELEASE.jar;F:mavenRepositoryorgapache omcatembed omcat-embed-core9.0.26 omcat-embed-core-9.0.26.jar;F:mavenRepositoryorgapache omcatembed omcat-embed-el9.0.26 omcat-embed-el-9.0.26.jar;F:mavenRepositoryorgapache omcatembed omcat-embed-websocket9.0.26 omcat-embed-websocket-9.0.26.jar;F:mavenRepositoryorghibernatevalidatorhibernate-validator6.0.17.Finalhibernate-validator-6.0.17.Final.jar;F:mavenRepositoryjavaxvalidationvalidation-api2.0.1.Finalvalidation-api-2.0.1.Final.jar;F:mavenRepositoryorgjbossloggingjboss-logging3.3.3.Finaljboss-logging-3.3.3.Final.jar;F:mavenRepositorycomfasterxmlclassmate1.4.0classmate-1.4.0.jar;F:mavenRepositoryorgspringframeworkspring-web5.1.10.RELEASEspring-web-5.1.10.RELEASE.jar;F:mavenRepositoryorgspringframeworkspring-beans5.1.10.RELEASEspring-beans-5.1.10.RELEASE.jar;F:mavenRepositoryorgspringframeworkspring-webmvc5.1.10.RELEASEspring-webmvc-5.1.10.RELEASE.jar;F:mavenRepositoryorgspringframeworkspring-aop5.1.10.RELEASEspring-aop-5.1.10.RELEASE.jar;F:mavenRepositoryorgspringframeworkspring-context5.1.10.RELEASEspring-context-5.1.10.RELEASE.jar;F:mavenRepositoryorgspringframeworkspring-expression5.1.10.RELEASEspring-expression-5.1.10.RELEASE.jar;F:mavenRepositoryorgslf4jslf4j-api1.7.28slf4j-api-1.7.28.jar;F:mavenRepositoryorgspringframeworkspring-core5.1.10.RELEASEspring-core-5.1.10.RELEASE.jar;F:mavenRepositoryorgspringframeworkspring-jcl5.1.10.RELEASEspring-jcl-5.1.10.RELEASE.jar" com.example.demo.DemoApplication . ____ _ __ _ _ /\ / ___'_ __ _ _(_)_ __ __ _ ( ( )\___ | '_ | '_| | '_ / _` | \/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.9.RELEASE) 2019-10-30 12:19:08.662 INFO 8128 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on DESKTOP-T849CU0 with PID 8128 (G:IDEAdemo argetclasses started by wym in G:IDEAdemo) 2019-10-30 12:19:08.690 INFO 8128 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default 2019-10-30 12:19:13.139 INFO 8128 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2019-10-30 12:19:13.226 INFO 8128 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2019-10-30 12:19:13.227 INFO 8128 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.26] 2019-10-30 12:19:13.242 INFO 8128 --- [ main] o.a.catalina.core.AprLifecycleListener : An older version [1.2.21] of the APR based Apache Tomcat Native library is installed, while Tomcat recommends a minimum version of [1.2.23] 2019-10-30 12:19:13.242 INFO 8128 --- [ main] o.a.catalina.core.AprLifecycleListener : Loaded APR based Apache Tomcat Native library [1.2.21] using APR version [1.6.5]. 2019-10-30 12:19:13.242 INFO 8128 --- [ main] o.a.catalina.core.AprLifecycleListener : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. 2019-10-30 12:19:13.243 INFO 8128 --- [ main] o.a.catalina.core.AprLifecycleListener : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true] 2019-10-30 12:19:13.334 INFO 8128 --- [ main] o.a.catalina.core.AprLifecycleListener : OpenSSL successfully initialized [OpenSSL 1.1.1a 20 Nov 2018] 2019-10-30 12:19:13.594 INFO 8128 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-10-30 12:19:13.594 INFO 8128 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4713 ms 2019-10-30 12:19:14.195 INFO 8128 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2019-10-30 12:19:14.594 INFO 8128 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2019-10-30 12:19:14.601 INFO 8128 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 8.051 seconds (JVM running for 14.462) 2019-10-30 12:20:53.101 INFO 8128 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2019-10-30 12:20:53.101 INFO 8128 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2019-10-30 12:20:53.116 INFO 8128 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 15 ms
运行成功后,浏览器中输入http://localhost:8080/hello
从上面的过程和SSM搭建HelloWorld项目的对比来看,SpringBoot不必配置任何xml文件,过程大大简化,使得程序员能够更加专注于程序的业务逻辑。Springboot甚至内置了tomcat,也不需要我们手动配置。