功能就是:在浏览器输入网址http://localhost:8080/hello即可访问Hello World
- 首先创建一个maven工程:就最简单的maven工程即可,在pom.xml编写Spring Boot的依赖:实际为官方文档摘抄而来
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.8.RELEASE</version> </parent> <!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
- 编写主程序:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class HelloWorld { public static void main(String[] args) { SpringApplication.run(HelloWorld.class,args); } }
@SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
- 编写相关的Controller
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloWordController { @ResponseBody @RequestMapping("/hello") public String hello(){ return "Hello World!!!"; } }
controller的编写与使用Spring MVC 的时候是一样的。
- 运行主程序:直接运行主程序即可,出现以下结果为成功
"C:\Program Files\Java\jdk1.8.0_144\bin\java" "-javaagent:D:\IntelliJ IDEA\lib\idea_rt.jar=5733:D:\IntelliJ IDEA\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_144\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\rt.jar;D:\IntelliJ IDEA Proj\holleWorld\target\classes;E:\maven\repository\org\springframework\boot\spring-boot-starter-web\2.0.8.RELEASE\spring-boot-starter-web-2.0.8.RELEASE.jar;E:\maven\repository\org\springframework\boot\spring-boot-starter\2.0.8.RELEASE\spring-boot-starter-2.0.8.RELEASE.jar;E:\maven\repository\org\springframework\boot\spring-boot\2.0.8.RELEASE\spring-boot-2.0.8.RELEASE.jar;E:\maven\repository\org\springframework\boot\spring-boot-autoconfigure\2.0.8.RELEASE\spring-boot-autoconfigure-2.0.8.RELEASE.jar;E:\maven\repository\org\springframework\boot\spring-boot-starter-logging\2.0.8.RELEASE\spring-boot-starter-logging-2.0.8.RELEASE.jar;E:\maven\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;E:\maven\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;E:\maven\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;E:\maven\repository\org\apache\logging\log4j\log4j-to-slf4j\2.10.0\log4j-to-slf4j-2.10.0.jar;E:\maven\repository\org\apache\logging\log4j\log4j-api\2.10.0\log4j-api-2.10.0.jar;E:\maven\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;E:\maven\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;E:\maven\repository\org\springframework\spring-core\5.0.12.RELEASE\spring-core-5.0.12.RELEASE.jar;E:\maven\repository\org\springframework\spring-jcl\5.0.12.RELEASE\spring-jcl-5.0.12.RELEASE.jar;E:\maven\repository\org\yaml\snakeyaml\1.19\snakeyaml-1.19.jar;E:\maven\repository\org\springframework\boot\spring-boot-starter-json\2.0.8.RELEASE\spring-boot-starter-json-2.0.8.RELEASE.jar;E:\maven\repository\com\fasterxml\jackson\core\jackson-databind\2.9.8\jackson-databind-2.9.8.jar;E:\maven\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;E:\maven\repository\com\fasterxml\jackson\core\jackson-core\2.9.8\jackson-core-2.9.8.jar;E:\maven\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.8\jackson-datatype-jdk8-2.9.8.jar;E:\maven\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.8\jackson-datatype-jsr310-2.9.8.jar;E:\maven\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.8\jackson-module-parameter-names-2.9.8.jar;E:\maven\repository\org\springframework\boot\spring-boot-starter-tomcat\2.0.8.RELEASE\spring-boot-starter-tomcat-2.0.8.RELEASE.jar;E:\maven\repository\org\apache\tomcat\embed\tomcat-embed-core\8.5.37\tomcat-embed-core-8.5.37.jar;E:\maven\repository\org\apache\tomcat\embed\tomcat-embed-el\8.5.37\tomcat-embed-el-8.5.37.jar;E:\maven\repository\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.37\tomcat-embed-websocket-8.5.37.jar;E:\maven\repository\org\hibernate\validator\hibernate-validator\6.0.14.Final\hibernate-validator-6.0.14.Final.jar;E:\maven\repository\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;E:\maven\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;E:\maven\repository\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;E:\maven\repository\org\springframework\spring-web\5.0.12.RELEASE\spring-web-5.0.12.RELEASE.jar;E:\maven\repository\org\springframework\spring-beans\5.0.12.RELEASE\spring-beans-5.0.12.RELEASE.jar;E:\maven\repository\org\springframework\spring-webmvc\5.0.12.RELEASE\spring-webmvc-5.0.12.RELEASE.jar;E:\maven\repository\org\springframework\spring-aop\5.0.12.RELEASE\spring-aop-5.0.12.RELEASE.jar;E:\maven\repository\org\springframework\spring-context\5.0.12.RELEASE\spring-context-5.0.12.RELEASE.jar;E:\maven\repository\org\springframework\spring-expression\5.0.12.RELEASE\spring-expression-5.0.12.RELEASE.jar" com.hzp.HelloWorld . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.8.RELEASE) 2019-02-14 12:33:54.903 INFO 7508 --- [ main] com.hzp.HelloWorld : Starting HelloWorld on hzp-PC with PID 7508 (started by hzp in D:\IntelliJ IDEA Proj\holleWorld) 2019-02-14 12:33:54.907 INFO 7508 --- [ main] com.hzp.HelloWorld : No active profile set, falling back to default profiles: default 2019-02-14 12:33:55.094 INFO 7508 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6ee12bac: startup date [Thu Feb 14 12:33:55 CST 2019]; root of context hierarchy 2019-02-14 12:33:57.404 INFO 7508 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2019-02-14 12:33:57.461 INFO 7508 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2019-02-14 12:33:57.462 INFO 7508 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.37 2019-02-14 12:33:57.478 INFO 7508 --- [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: [C:\Program Files\Java\jdk1.8.0_144\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\Program Files\MySQL\MySQL Server 5.5\bin;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;D:\eclipse\Apache\maven\bin;D:\c++\Qt5.3.1\Tools\mingw482_32\bin;D:\c++\Qt5.3.1\5.3\mingw482_32\bin;C:\Program Files\mingw-w64\x86_64-5.3.0-win32-seh-rt_v4-rev0\mingw64\bin;F:\Program Files\Git\cmd;F:\Program Files\Tortoise\bin;.] 2019-02-14 12:33:57.651 INFO 7508 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-02-14 12:33:57.651 INFO 7508 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2562 ms 2019-02-14 12:33:57.804 INFO 7508 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 2019-02-14 12:33:57.812 INFO 7508 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2019-02-14 12:33:57.813 INFO 7508 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2019-02-14 12:33:57.813 INFO 7508 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2019-02-14 12:33:57.813 INFO 7508 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2019-02-14 12:33:58.311 INFO 7508 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2019-02-14 12:33:58.643 INFO 7508 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6ee12bac: startup date [Thu Feb 14 12:33:55 CST 2019]; root of context hierarchy 2019-02-14 12:33:58.737 INFO 7508 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/holle]}" onto public java.lang.String com.hzp.controller.HolleWordController.hello() 2019-02-14 12:33:58.742 INFO 7508 --- [ 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) 2019-02-14 12:33:58.743 INFO 7508 --- [ 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) 2019-02-14 12:33:58.774 INFO 7508 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2019-02-14 12:33:58.775 INFO 7508 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2019-02-14 12:33:59.060 INFO 7508 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2019-02-14 12:33:59.124 INFO 7508 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2019-02-14 12:33:59.129 INFO 7508 --- [ main] com.hzp.HelloWorld : Started HelloWorld in 6.297 seconds (JVM running for 8.469) 2019-02-14 12:34:33.768 INFO 7508 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet' 2019-02-14 12:34:33.768 INFO 7508 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started 2019-02-14 12:34:33.786 INFO 7508 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 18 ms
在浏览器上输入:http://localhost:8080/hello即可访问
-
出现 Hello World