SpringBoot 系列教程(HelloWorld)

本例描述

通过以下范例,可以快速上手使用SpringBoot框架。先来一个HelloWorld小工程。

开发工具

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

测试工具

本例可使用 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(存放启动类代码)

  2. 创建控制器类 HelloWorldController 类,代码如下:

@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 ! (表示程序完成)

猜你喜欢

转载自my.oschina.net/u/136229/blog/1593139