Spring boot之Hello World

1.开发准备

开发环境JDK 1.8

开发工具(Eclipse)

项目管理工具( Maven )

2.开发步骤

《1》创建Maven Project (spring-boot-hello)

《2》pom.xml导入jar

<!-- spring boot 父节点依赖,引入这个之后相关的引入就不需要添加version配置,spring boot会自动选择最合适的版本进行添加。 -->

<parent>

  <groupId>org.springframework.boot</groupId>

  <artifactId>spring-boot-starter-parent</artifactId>

  <version>1.4.1.RELEASE</version>

</parent>

  <!--  java.version 指定jdk版本号: -->

  <java.version>1.8</java.version>

  <!--添加spring-boot-starter-web依赖 -->

  <dependency>

          <groupId>org.springframework.boot</groupId>

          <artifactId>spring-boot-starter-web</artifactId>

 </dependency>

《3》新建一个Controller类

《4》新建启动类(App – Main方法)

《5》测试代码

3.项目构建代码

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.kfit</groupId>
  <artifactId>spring-boot-hello</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>spring-boot-hello</name>
  <url>http://maven.apache.org</url>
 
    <!-- 
     spring boot 父节点依赖,引入这个之后相关的引入就不需要添加version配置,spring boot会自动选择最合适的版本进行添加。
    -->
    <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>
    <!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 -->
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
          <!-- 
              spring-boot-starter-web: MVC,AOP的依赖包....
           -->
          <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <!-- 
                    <version></version>
                    由于我们在上面指定了 parent(spring boot)
                 -->
        </dependency>
        
  </dependencies> 
</project>
Controller类
package com.kfit;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 在这里我们使用RestController  (等待于 @Controller 和 @RequestBody)
 */
@RestController
public class HelloController {
    
    /**
     * 在这里我们使用@RequestMapping 建立请求映射:
     * http://127.0.0.1:8080/hello
     * @return
     */
    @RequestMapping("/hello")
    public String hello(){
        return "hello-2016-12-11.v.0";
    }
}

启动类

package com.kfit;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 

/**
 * 在这里我们使用@SpringBootApplication指定这是一个 spring boot的应用程序.
 */
 
@SpringBootApplication
public class App{
    public static void main(String[] args) {
        /*
         * 在main方法进行启动我们的应用程序.
         */
        SpringApplication.run(App.class, args);
    }
}

 总结

1、第1节 Spring Boot之Hello World
-------------------------------------------------
1、新建一个HelloController;

2、编写Spring Boot 启动类;

3、进行访问测试,默认的端口号是8080;

 

 

 

posted @ 2018-07-05 10:42  曹英杰  阅读(128)  评论(0编辑  收藏  举报