阅读462 返回首页    go 阿里云 go 技术社区[云栖]


Spring Boot 入门篇

Spring Boot 相比Spring Framework框架的繁杂配置(各种XML配置与代码CP)更轻量化,内嵌Web 容器(默认Tomcat)启动应用只需一个类即可;使Spring开发者能更快的入门,大大的提高了开发效率, 下文开始演示Spring Boot的入门(英文与技术基础好的同学,可以直进入官网看demo学习)。

开发环境说明:

  • java version "1.8.0_91"
  • Spring Boot 1.5.2.RELEASE 用例亲自调试通过,放心入坑。
  1. Maven构建POM #代码引用 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencies> <!-- 引入Web模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
  2. Gradle构建POM #代码引用 dependencies { compile("org.springframework.boot:spring-boot-starter-web:1.5.2.RELEASE") } #项目目录结构

Paste_Image.png

新建类

hello/SampleController.java

代码引用

package hello;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}

启动应用主程序SampleController

启动日志

  1. 启动默认用了Tomcat 端口8080
  2. Spring Boot 版本号1.5.2
  3. 无需任何配置文件申明

Paste_Image.png

  • 下面开始使用Postman来请求应用,结果如图
  • 也可以直接在浏览器上面输入:https://localhost:8080/

Paste_Image.png

'Hello World!'

本项目演示了,通过Maven 构建项目,引用Spring Boot中

spring-boot-starter-parent
spring-boot-starter-web

实现了基础的Web应用,处理简单的请求,下篇开始引用application.properties 或 application.yml 实现修改应用默认端口与数据库访问。

参考资料:

https://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/

最后更新:2017-04-01 16:41:01

  上一篇:go 和黑客在一起的365天论攻与防
  下一篇:go 如何在阿里云从零搭建一个防入侵体系