您好,SpringBoot

2016-09-04  本文已影响0人  keltoy

从今天开始学习Spring Boot 微框架,今天先建立一个简单的Spring Boot 的 hello world。


环境及工具

本人的环境为:

建立项目

建立一个Maven工程

  1. 打开IDEA,选中一个Maven工程:


    Maven工程

    这里可以不使用 maven 自带的 archetype,直接点击右下角的 Next 按钮即可。

  2. 填写 groupId 和 atifactId。
  3. 填写 项目名称和项目目录。

修改 pom.xml 文件

  1. 点击 pom.xml。


    pom.xml的位置
  2. 修改 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.eye.toy</groupId>
    <artifactId>hello-spring-boot</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>hello-spring-boot</name>
    <description>A demo of Spring Web </description>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
    </parent>
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    </properties>
    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    </dependencies>
    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>
    </project>

  3. 在 src/java/ 下新增一个 Application.java 文件,并填写代码。


    Application的代码

    这么写貌似有点像Django...
    代码如下:
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

     /** 
      * Created by toy on 9/4/16.       
      */
    
     @RestController
     @EnableAutoConfiguration
     class Application {   
      
         @RequestMapping("/")
         public String greeting() {    
             return "Hello World";   
         } 
    
        public static void main(String[] args) {   
            SpringApplication.run(Application.class, args);  
        }
     }
    
  4. 运行程序。
    点击 Edit Configurations... 编辑Web选项。

    点击 Edit Configurations...
    点击左上角的 "+" 添加一个 Maven Configuration。
    添加一个 maven configuration
    按如下填写 maven 参数。
    Paste_Image.png
    注意: Working directory 是自己的项目目录。
    之后点击于行按钮,在浏览器中打开 [http://localhost:808] (http://localhost:8080)就完成了。

嗯嗯,结果就是这样。


hello world

还有一种比较简单的构建方法点击 SPRING INITIALIZR,就可以建立一个初始化好的spring boot 。


第一次写,感觉还行。
感谢chirs

上一篇 下一篇

猜你喜欢

热点阅读