本节内容包含SpringBoot的开发环境配置以及第一个HelloWorld
准备环境:jdk1.8 + maven3.3.9 + eclipse
1. 新建Maven项目
2. 修改pom.xml文件
引入父类包以及spring-web的依赖包
<!-- 引用 springboot-parent,实现了很多jar依赖管理 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <!-- 由于sprintboot默认引用了springmvc,所以这里只需要引用spring-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
3. 在src/main/java目录下创建controller包,以及TestController文件
TestController.java文件内容如下:
package com.king.controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @EnableAutoConfiguration public class TestController { @RequestMapping("/hello") public String hello(){ return "Hello world!"; } public static void main(String[] args) { SpringApplication.run(TestController.class, args); } }
4. 在TestController中右键run as Java Application
然后在控制台中看到如下信息:
. ____ _ __ _ _ /\ / ___'_ __ _ _(_)_ __ __ _ ( ( )\___ | '_ | '_| | '_ / _` | \/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.9.RELEASE) 2018-01-28 22:45:03.463 INFO 171392 --- [ main] com.king.controller.TestController : Starting TestController on KingWang with PID 171392 (D:workspaceworkspace_j2eespringboot argetclasses started by Administrator in D:workspaceworkspace_j2eespringboot) 2018-01-28 22:45:03.467 INFO 171392 --- [ main] com.king.controller.TestController : No active profile set, falling back to default profiles: default 2018-01-28 22:45:03.542 INFO 171392 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5e955596: startup date [Sun Jan 28 22:45:03 CST 2018]; root of context hierarchy 2018-01-28 22:45:06.660 INFO 171392 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2018-01-28 22:45:06.684 INFO 171392 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-01-28 22:45:06.690 INFO 171392 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.23 2018-01-28 22:45:06.919 INFO 171392 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-01-28 22:45:06.920 INFO 171392 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3383 ms 2018-01-28 22:45:07.296 INFO 171392 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2018-01-28 22:45:07.303 INFO 171392 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2018-01-28 22:45:07.307 INFO 171392 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2018-01-28 22:45:07.314 INFO 171392 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2018-01-28 22:45:07.315 INFO 171392 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2018-01-28 22:45:08.056 INFO 171392 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5e955596: startup date [Sun Jan 28 22:45:03 CST 2018]; root of context hierarchy 2018-01-28 22:45:08.189 INFO 171392 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.king.controller.TestController.hello() 2018-01-28 22:45:08.199 INFO 171392 --- [ 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) 2018-01-28 22:45:08.199 INFO 171392 --- [ 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) 2018-01-28 22:45:08.250 INFO 171392 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-01-28 22:45:08.250 INFO 171392 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-01-28 22:45:08.325 INFO 171392 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-01-28 22:45:08.849 INFO 171392 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-01-28 22:45:09.071 INFO 171392 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2018-01-28 22:45:09.079 INFO 171392 --- [ main] com.king.controller.TestController : Started TestController in 6.343 seconds (JVM running for 7.083)
5. 在浏览器中输入http://localhost:8080/hello即可
到此,一个简单的springboot项目完成。。。
使用springboot开开发java项目居然如此简单,
省去了ssm中的那些繁琐的xml配置。
内置了tomcat服务器,
直接生成一个可执行的jar文件,
所以我们直接通过run命令即可执行。
好了,夸奖springboot到此。