您的位置:首页 > 编程语言 > Java开发

SpringBoot 快速入门

2017-01-19 10:02 369 查看
需求描述:使用SpringBoot编写HelloWorld入门程序

新建Maven项目

在pom文件中添加以下依赖

<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.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.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableAutoConfiguration
public class QuickstartApplication {

@RequestMapping("/")
@ResponseBody
String home(){
return "Hello World!!!";
}

public static void main(String[] args) throws Exception {
SpringApplication.run(QuickstartApplication.class, args);
}

}


测试运行

选中Main方法–>右键Run as –> Spring Boot App(基于Spring的sts-bundle的IDE)或者Java Application. 出现如下的日志信息且无报错信息即为启动正常。

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

2017-01-19 09:55:45.757  INFO 8256 --- [           main] c.t.s.quickstart.QuickstartApplication   : Starting QuickstartApplication on EZ-20161228YDGX with PID 8256 (D:\tyrone-workspace\tyrone-springboot-quickstart\target\classes started by Administrator in D:\tyrone-workspace\tyrone-springboot-quickstart)
2017-01-19 09:55:45.762  INFO 8256 --- [           main] c.t.s.quickstart.QuickstartApplication   : No active profile set, falling back to default profiles: default
2017-01-19 09:55:45.972  INFO 8256 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6379eb: startup date [Thu Jan 19 09:55:45 CST 2017]; root of context hierarchy
2017-01-19 09:55:48.395  INFO 8256 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-01-19 09:55:48.411  INFO 8256 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2017-01-19 09:55:48.412  INFO 8256 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.6
2017-01-19 09:55:48.539  INFO 8256 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-01-19 09:55:48.539  INFO 8256 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2571 ms
2017-01-19 09:55:48.745  INFO 8256 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-01-19 09:55:48.751  INFO 8256 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-01-19 09:55:48.752  INFO 8256 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-01-19 09:55:48.752  INFO 8256 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-01-19 09:55:48.752  INFO 8256 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-01-19 09:55:49.191  INFO 8256 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6379eb: startup date [Thu Jan 19 09:55:45 CST 2017]; root of context hierarchy
2017-01-19 09:55:49.503  INFO 8256 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String cn.tyrone.springboot.quickstart.QuickstartApplication.home()
2017-01-19 09:55:49.509  INFO 8256 --- [           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)
2017-01-19 09:55:49.509  INFO 8256 --- [           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)
2017-01-19 09:55:49.554  INFO 8256 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-01-19 09:55:49.554  INFO 8256 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-01-19 09:55:49.605  INFO 8256 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-01-19 09:55:49.853  INFO 8256 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-01-19 09:55:49.942  INFO 8256 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-01-19 09:55:49.948  INFO 8256 --- [           main] c.t.s.quickstart.QuickstartApplication   : Started QuickstartApplication in 5.044 seconds (JVM running for 6.027)


列表内容

打开浏览器,输入http://localhost:8080/进行验证。

若浏览器打印出启动类中的结果“Hello World”即正常。

参考链接:

http://projects.spring.io/spring-boot/

可能会遇到的问题:

http://blog.csdn.net/mynameissls/article/details/54603148
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SpringBoot