SpringBoot 系列教程(HelloWorld)

本例描述

经过如下范例,能够快速上手使用SpringBoot框架。先来一个HelloWorld小工程。web

开发工具

本系列教程均采用 IDEA 做为开发工具,JDK 为 1.8spring

测试工具

本例可以使用 PostMan工具来进行测试。PostMan 官网地址 可进行下载。浏览器

开发步骤

  1. 打开IDEA,建立工程 HelloWorld(此处截图省略);
  2. 在POM文件中添加以下代码内容:
<!-- 添加SpringBoot 父工程依赖 -->
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/>
</parent>
<dependencies>
        <!-- springboot web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
  1. 建立包结构信息 com.test.helloworld.controller(存放控制器代码) 、 com.test.helloworld.app(存放启动类代码)springboot

  2. 建立控制器类 HelloWorldController 类,代码以下:app

@RestController
public class HelloWorldController {

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

HelloWorldController类经过注解 @ResController 可将此类进行转变,转变成Rest服务。类中还有一个sayHello函数,此函数经过注解 @RequestMapping 是一个用来处理请求地址映射的注解。框架

  1. 建立启动类 Application 类,代码以下:
@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        /**
         * 启动服务
         */
        SpringApplication.run(Application.class, args);
    }
}
  1. 启动 Application 类,成功后 经过浏览器访问 http://localhost:8080/函数

  2. 界面会呈现出 hello world ! (表示程序完成)spring-boot

相关文章
相关标签/搜索