springboot学习(二) 第一个springboot项目:Hello World!

1、简介

  可以像使用其他java标准库那样使用spriongboot,只需简单地在你的classpath下包含正确的 spring-boot-*.jar 文件。springboot不需要集成任何特殊的工具,所以可以使用任意的ide。

  虽然可以拷贝springboot jars,但是,推荐使用一个支持依赖管理的构建工具,比如maven。

 

2、创建maven项目

  spring boot支持maven3.2及以上版本,版本过低需要升级maven版本。

  构建玩maven项目过后,修改pom.xml,导入spring boot的相关依赖:

<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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>cn.origal</groupId>
    <artifactId>springbootdemo</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>springbootdemo Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <dependencies>

        <!--   基本的web依赖     -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>springbootdemo</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

  说明:  spring-boot-starter-parent 是一个很不错的使用方式,通过它基本不再需要其他配置。

 

3、编写代码

  创建Example.java:

 1 package cn.origal;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RestController;
 7 
 8 @RestController
 9 @EnableAutoConfiguration
10 public class Example {
11 
12     @RequestMapping("/")
13     public String hello() {
14         return "Hello World!";
15     }
16 
17     public static void main(String[] args) {
18         SpringApplication.run(Example.class, args);
19     }
20 }

  注解意义说明:

    ①  @RestController: 这是一个构造型(stereotype)注解,它为阅读代码的人们提供建议,当处理进来的web请求时,Spring会询问它查找对应的地址

    ②  @RequestMapping:  提供路由信息,它告诉Spring任何来自"/"路径的HTTP请求都应该被映射到 hello方法

    ③  @EnableAutoConfiguration:  这个注解告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring

 

4、运行项目

  执行main方法运行项目后,会看到控制台打印的日志信息:

 1   .   ____          _            __ _ _
 2  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
 3 ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 4  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
 5   '  |____| .__|_| |_|_| |_\__, | / / / /
 6  =========|_|==============|___/=/_/_/_/
 7  :: Spring Boot ::        (v1.5.9.RELEASE)
 8 
 9 ......
10 ......    Started Example in 5.174 seconds (JVM running for 6.114)

 

   在浏览器中输入网址 http://localhost:8080 ,会看到输出信息:

1 Hello World!

 

 

 至此,我们的第一个spring boot项目就完成了。

https://gitee.com/origalom/springbootdemo

posted on 2018-01-13 22:25  程序员AI  阅读(224)  评论(0编辑  收藏  举报

导航