1,首先创建一个maven工程

2,在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>
    <groupId>com.queen.springboot</groupId>
    <artifactId>SpringBootHelloWorld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.7</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

我们引入了Spring boot的父节点依赖,引入这个之后相关的引用就不需要添加version配置了,Spring boot会自动选择最合适的版本进行添加。
环境中我们指定JDK的版本为1.7,默认是1.6
导入spring-boot-starter-web依赖这个地方我们没有指定版本号,是因为上面我们配置了parent,所以不用添加了。

♦编写控制类SpringBootHelloWorld

/**
 * @author queen
 * @since 2017-07-16
 *
 */
@RestController
public class HelloWorldController {

    @RequestMapping("/hello")
    public String sayHello() {
        return "helloWorld";
    }
}

这里我们使用的@RestController注解等价于@Controller和@ResponseBody注解,使用@RequestMapping建立请求映射。

♦编写启动类APP

/**
 * @author queen
 * @since 2017-07-16
 *
 */
@SpringBootApplication
public class APP {

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

}

使用@SpringBootApplication注解定义APP是一个SpringBoot类型的应用,在main方法中使用SpringApplication的run方法启动我们的程序。

♦右键Run As -> Java Application,启动应用,控制台启动日志如下:

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

2017-07-16 10:31:07.706  INFO 12108 --- [           main] com.queen.controller.APP                 : Starting APP on Lenovo-PC with PID 12108 (E:\queen\SpringBootHelloWorld\target\classes started by think in E:\queen\SpringBootHelloWorld)
2017-07-16 10:31:07.709  INFO 12108 --- [           main] com.queen.controller.APP                 : No active profile set, falling back to default profiles: default
2017-07-16 10:31:07.814  INFO 12108 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6568614: startup date [Sun Jul 16 10:31:07 CST 2017]; root of context hierarchy
2017-07-16 10:31:10.086  INFO 12108 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-07-16 10:31:10.099  INFO 12108 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2017-07-16 10:31:10.101  INFO 12108 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.5
2017-07-16 10:31:10.201  INFO 12108 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-07-16 10:31:10.202  INFO 12108 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2392 ms
2017-07-16 10:31:10.411  INFO 12108 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-07-16 10:31:10.415  INFO 12108 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-07-16 10:31:10.417  INFO 12108 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-07-16 10:31:10.417  INFO 12108 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-07-16 10:31:10.417  INFO 12108 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-07-16 10:31:10.812  INFO 12108 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6568614: startup date [Sun Jul 16 10:31:07 CST 2017]; root of context hierarchy
2017-07-16 10:31:10.906  INFO 12108 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.queen.controller.HelloWorldController.sayHello()
2017-07-16 10:31:10.913  INFO 12108 --- [           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-16 10:31:10.913  INFO 12108 --- [           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-16 10:31:10.965  INFO 12108 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-16 10:31:10.966  INFO 12108 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-16 10:31:11.013  INFO 12108 --- [           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-16 10:31:11.320  INFO 12108 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-07-16 10:31:11.391  INFO 12108 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-07-16 10:31:11.398  INFO 12108 --- [           main] com.queen.controller.APP                 : Started APP in 4.301 seconds (JVM running for 4.619)

启动后默认端口号是8080

♦使用http://localhost:8080/hello看是否能够访问我们的应用
springboot第一个demo_springboot教程

访问成功。
至此,我们使用Spring boot搭建的第一个HelloWorld小程序完成。

博客地址:http://www.marsitman.com/springboot/springboot_helloworld.html
版权声明:本文为博主原创文章,允许转载,但转载必须标明出处。