SpringBoot的第一个程序helloworld
一开始编写第一个springboot程序helloworld时候,就会觉得不知所措,然后根据网上的一些文档看了一下,真的超级简单.入乡随俗,贴一下优点:Spring Boot的主要优点:为所有Spring开发者更快的入门开箱即用,提供各种默认配置来简化项目配置内嵌式容器简化Web项目没有冗余代码生成和XML配置的要求好了,言归正传,我这边使用的时idea,第一步:创建M
一开始编写第一个springboot程序helloworld时候,就会觉得不知所措,然后根据网上的一些文档看了一下,真的超级简单.入乡随俗,贴一下优点:
Spring Boot的主要优点:
- 为所有Spring开发者更快的入门
- 开箱即用,提供各种默认配置来简化项目配置
- 内嵌式容器简化Web项目
- 没有冗余代码生成和XML配置的要求
好了,言归正传,我这边使用的时idea,
第一步:创建Maven项目,可以使用quackstart,也可以使用webapp,
第二步:添加依赖和插件
2.1.设置spring boot的parent
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> </parent>
说明:Spring boot的项目必须要将parent设置为spring boot的parent,该parent包含了大量默认的配置,大大简化了我们的开发。
2.2.导入spring boot的web支持
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2.3.添加Spring boot的插件(这个可有可无,测试不加也没问题)
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
1.1.4.编写第一个Spring Boot的应用
@Controller @SpringBootApplication @Configuration public class HelloApplication { @RequestMapping("hello") @ResponseBody public String hello(){ return "hello world!"; } public static void main(String[] args) { SpringApplication.run(HelloApplication.class, args); } }
代码说明:
1、@SpringBootApplication:Spring Boot项目的核心注解,主要目的是开启自动配置。;
2、@Configuration:这是一个配置Spring的配置类;
3、@Controller:标明这是一个SpringMVC的Controller控制器,知道springmvc的就不用解释了;
4、main方法:在main方法中启动一个应用,即:这个应用的入口;
先运行主函数结果:
2018-01-02 17:06:42.928 INFO 3868 --- [ main] m.java.com.springboot.HelloApplication : Starting HelloApplication on jx with PID 3868 (E:\Files\IDEASpace\helloSpringBoot\target\classes started by jx in E:\Files\IDEASpace\helloSpringBoot)
2018-01-02 17:06:42.934 INFO 3868 --- [ main] m.java.com.springboot.HelloApplication : No active profile set, falling back to default profiles: default
2018-01-02 17:06:43.058 INFO 3868 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2429c748: startup date [Tue Jan 02 17:06:43 CST 2018]; root of context hierarchy
2018-01-02 17:06:44.730 INFO 3868 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2018-01-02 17:06:44.740 INFO 3868 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-01-02 17:06:44.741 INFO 3868 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.15
2018-01-02 17:06:44.848 INFO 3868 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-01-02 17:06:44.848 INFO 3868 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1793 ms
2018-01-02 17:06:45.050 INFO 3868 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2018-01-02 17:06:45.055 INFO 3868 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-01-02 17:06:45.055 INFO 3868 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-01-02 17:06:45.055 INFO 3868 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-01-02 17:06:45.055 INFO 3868 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-01-02 17:06:45.704 INFO 3868 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2429c748: startup date [Tue Jan 02 17:06:43 CST 2018]; root of context hierarchy
2018-01-02 17:06:45.769 INFO 3868 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String main.java.com.springboot.HelloApplication.hello()
2018-01-02 17:06:45.774 INFO 3868 --- [ 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-02 17:06:45.774 INFO 3868 --- [ 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-02 17:06:45.812 INFO 3868 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-01-02 17:06:45.812 INFO 3868 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-01-02 17:06:45.865 INFO 3868 --- [ 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-02 17:06:46.182 INFO 3868 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-01-02 17:06:46.311 INFO 3868 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-01-02 17:06:46.319 INFO 3868 --- [ main] m.java.com.springboot.HelloApplication : Started HelloApplication in 3.885 seconds (JVM running for 4.344)
-
这里边显示了使用的tomcat版本号,接口号等信息
-
最后就可以直接在浏览器访问"localhost:8080/hello"了.是不是觉得很简单,都是开始自己想多了.
更多推荐
所有评论(0)