第一步:使用Eclipse创建maven工程SpringBootFirst:工程目录如下
第二步:编写依赖文件pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.xzh.springboot</groupId>
<artifactId>SpringBoot-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.xzh</groupId>
<artifactId>SpringBootFirst</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>SpringBootFirst</name>
<description>SpringBootFirst for 20170717</description> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
编写web控制器:SampleController
package me.hello;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*; @Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home()
{
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}
第三步:右击SpringBootFirst工程,选择如下图运行
运行结果如下:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.4.RELEASE) 2017-07-24 11:25:35.788 INFO 8760 --- [ main] me.hello.SampleController : Starting SampleController on Lenovo-PC with PID 8760 (F:\eclipseworkspace\SpringBootFirst\target\classes started by hengliu in F:\eclipseworkspace\SpringBootFirst)
2017-07-24 11:25:35.791 INFO 8760 --- [ main] me.hello.SampleController : No active profile set, falling back to default profiles: default
2017-07-24 11:25:35.849 INFO 8760 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1e4a7dd4: startup date [Mon Jul 24 11:25:35 CST 2017]; root of context hierarchy
2017-07-24 11:25:38.608 INFO 8760 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-07-24 11:25:38.642 INFO 8760 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2017-07-24 11:25:38.643 INFO 8760 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.15
2017-07-24 11:25:38.849 INFO 8760 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-07-24 11:25:38.849 INFO 8760 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3010 ms
2017-07-24 11:25:39.071 INFO 8760 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-07-24 11:25:39.077 INFO 8760 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-07-24 11:25:39.078 INFO 8760 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-07-24 11:25:39.079 INFO 8760 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-07-24 11:25:39.079 INFO 8760 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-07-24 11:25:39.719 INFO 8760 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1e4a7dd4: startup date [Mon Jul 24 11:25:35 CST 2017]; root of context hierarchy
2017-07-24 11:25:39.851 INFO 8760 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String me.hello.SampleController.home()
2017-07-24 11:25:39.855 INFO 8760 --- [ 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-07-24 11:25:39.856 INFO 8760 --- [ 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-07-24 11:25:39.898 INFO 8760 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-24 11:25:39.899 INFO 8760 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-24 11:25:39.954 INFO 8760 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-24 11:25:40.144 INFO 8760 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-07-24 11:25:40.297 INFO 8760 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-07-24 11:25:40.304 INFO 8760 --- [ main] me.hello.SampleController : Started SampleController in 5.157 seconds (JVM running for 6.183)
2017-07-24 11:26:36.857 INFO 8760 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-07-24 11:26:36.858 INFO 8760 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-07-24 11:26:36.880 INFO 8760 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 22 ms
说明运行成功,启动了自带的Tomcat服务器,然后打开浏览器输入:http://localhost:8080/
运行结果如下图: