本文所属【知识林】:http://www.zslin.com/web/article/detail/7

上一篇说了Springboot的那么多好处,说得有多么的高效,现在先构造一个Helloworld来看一下具体的效果。

在Springboot的开发中本人比较喜欢使用IDEA开发工具(构建比较方便),点击这里可进入IDEA的下载页面

第一步、构造项目

  • 点击File -> New -> Project
  • 左边选择Maven,右边什么都不用选择,点击Next或按Alt+N
  • 在GroupId处输入包名,如:com.zslin(一般用域名反写)
  • 在ArtifactId处输入工程ID,如:study01(用于项目中显示的名称),点击Next或按Alt+N
  • 在Project name处输入项目名称,如:study01(用于磁盘中存储的文件夹名)
  • 在Project location处输入或选择项目存放在磁盘的路径,点击Finish或按Alt+F

完成项目构建。

第二步、配置pom.xml

项目构建完成后在pom.xml(Maven配置文件)文件中引入Springboot的配置。

1、 引入spring-boot-starter-parent,这里使用版本为1.4.0.RELEASE

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

2、 引入最基础的依赖
由于继承了starter-parent,且指定了相应版本,所以在引入Springboot相关的依赖时不再需要指定版本,Springboot会自动寻找最合适的版本。

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

完成的pom.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<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.zslin</groupId>
    <artifactId>study01</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

第三步、创建测试类

package com.zslin.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by zslin.com on 2016/10/13.
 */
@RestController
public class IndexController {

    @RequestMapping(value = "index")
    public String index() {

        return "Hello World!!!";
    }
}
注意:

1、这一步可以不要,但为了更友好的显示效果,所以还是写了。
2、该java文件所在的包是由第一步中的GroupId处输入的包名而定的。

第四步、创建启动文件

新建一个Java对象命名为RootApplication.java(名称可自己定义)。
全代码如下:

package com.zslin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Created by zslin.com on 2016/10/13.
 */
@SpringBootApplication
public class RootApplication {

    public static void main(String [] args) {
        SpringApplication.run(RootApplication.class, args);
    }
}
注意:

1、`RootApplication.java`需要在`com.zslin`包里,这个包是由第一步中的GroupId处输入的包名而写;
2、`RootApplication.java`在类名上加注释`@SpringBootApplication`;
3、在该类里的main方法上启动运行。

第五步、运行main方法


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

2016-10-13 00:52:04.534  INFO 6828 --- [           main] com.zslin.RootApplication                : Starting RootApplication on zsl with PID 6828 (E:\idea\2016_zslin\study01\target\classes started by zsl-pc in E:\idea\2016_zslin\study01)
2016-10-13 00:52:04.538  INFO 6828 --- [           main] com.zslin.RootApplication                : No active profile set, falling back to default profiles: default
2016-10-13 00:52:04.666  INFO 6828 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@9225652: startup date [Thu Oct 13 00:52:04 CST 2016]; root of context hierarchy
2016-10-13 00:52:07.340  INFO 6828 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-10-13 00:52:07.352  INFO 6828 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-10-13 00:52:07.353  INFO 6828 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.4
2016-10-13 00:52:07.473  INFO 6828 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-10-13 00:52:07.474  INFO 6828 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2815 ms
2016-10-13 00:52:07.664  INFO 6828 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2016-10-13 00:52:07.670  INFO 6828 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-10-13 00:52:07.671  INFO 6828 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-10-13 00:52:07.671  INFO 6828 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-10-13 00:52:07.671  INFO 6828 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2016-10-13 00:52:07.997  INFO 6828 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@9225652: startup date [Thu Oct 13 00:52:04 CST 2016]; root of context hierarchy
2016-10-13 00:52:08.065  INFO 6828 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/index]}" onto public java.lang.String com.zslin.controller.IndexController.index()
2016-10-13 00:52:08.070  INFO 6828 --- [           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)
2016-10-13 00:52:08.071  INFO 6828 --- [           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)
2016-10-13 00:52:08.105  INFO 6828 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-10-13 00:52:08.105  INFO 6828 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-10-13 00:52:08.148  INFO 6828 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-10-13 00:52:08.294  INFO 6828 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-10-13 00:52:08.359  INFO 6828 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-10-13 00:52:08.363  INFO 6828 --- [           main] com.zslin.RootApplication                : Started RootApplication in 4.436 seconds (JVM running for 4.958)

可以看到程序已成功运行,且默认端口是8080,此时可以在浏览器里输入地址查看效果:
http://localhost:8080/index,网页上显示:Hello World!!!就表示网站已成功搭建完成!


示例仓库:https://github.com/zsl131/spring-boot-test

示例代码:https://github.com/zsl131/spring-boot-test/tree/master/study01

本文所属【知识林】:http://www.zslin.com/web/article/detail/7
文章点评共:6 条
点评内容: 提交点评

06-11 点评:完美运行

03-20 点评:谢谢你的分享!!!

03-20 回复:不客气,有帮助就行

03-08 点评:非常不错

03-14 回复:谢谢

12-21 点评:maven和spring initializr区别

12-23 回复:目前只用过maven呢

12-06 点评:教程内容非常详细

12-07 回复:感谢你的肯定!

11-11 点评:很不错!!!

11-11 回复:谢谢你的肯定!