惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tailwind CSS Blog
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
The Cloudflare Blog
博客园 - 聂微东
博客园 - 司徒正美
量子位
博客园 - 三生石上(FineUI控件)
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
G
Google Developers Blog
Apple Machine Learning Research
Apple Machine Learning Research
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
Y
Y Combinator Blog
S
SegmentFault 最新的问题
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
有赞技术团队
有赞技术团队
A
About on SuperTechFans

少数派

派早报:Google 发布 Fitbit Air 等 - 少数派 「新人报到」確認需求,再開始 - 少数派 从 SOLO 独立开发者社区,我看到了越来越多开发者开始做自己的产品 - 少数派 我怎么管理那些"不常做,但总会忘"的生活事项 - 少数派 人形机器人量产元年,数据才是具身智能的“生死线” - 少数派 BuhoLaunchpad 高度还原 Mac 启动台:开发历程与思考 - 少数派 五年陪伴依然不舍,DIY 换壳后让罗技 MX Master 3 继续服役 - 少数派 新玩意 240|少数派的编辑们最近买了啥? - 少数派 一日一技|为什么你应该关闭 iOS 的键盘声音 - 少数派 我做了个插件和 Skills,一键提取任何网站的设计规范 Design.md - 少数派 住在三四线城市的你,该开始录播客了 - 少数派 甘南秘境,大白高国 - 少数派 AI的审美:谁让把我变成川内倫子 - 少数派 返工怎能不烦恼,打工人片单总有一部是你的「嘴替」 - 少数派 为了让「上厕所」更健康,我做了一个小工具 - 少数派 AI + Skill,能够让生成的文章去除 AI 味吗? - 少数派 新玩意|韶音OpenDots ONE 耳夹式耳机 - 少数派 《美满》| 在每一个春天的晚上相爱(362) - 少数派 新玩意|优篮子 PS01 MagSnap 磁吸支架 - 少数派 自我整合手记 | 我开始早睡了:用稳定规则,为自由托底 - 少数派 用龙虾(OpenClaw)两个多月,我最深的12个体会 - 少数派 听歌时间到,12 张你可能错过的 2025 华语乐坛好专辑 - 少数派 承诺能追吗 - 少数派 macOS 26启动台没了? 我做了个不一样的App启动器 - Keboard - 少数派 《四海为家的人》| INTJ对话INTJ(361) - 少数派 你发过的那些黑历史,是时候一次清干净了 - 少数派 新玩意:安安静静玩,越玩越专注:计客密码机 - 少数派 iPad 用户首次体验 Android 平板:vivo Pad6 Pro - 少数派 数据逻辑强 - 少数派 极北行+ | 一路向北,探访日本至北之地 | 001 - 少数派
Spring Boot 2.0.1 入门教程 - 少数派
2018-05-02 · via 少数派

简介

Spring Boot是Spring提供的一套基础配置环境,可以用来快速开发生产环境级别的产品。尤其适合开发微服务架构,省去了不少配置麻烦。比如用到Spring MVC时,只需把spring-boot-starter-web依赖添加到Maven依赖中即可。另外它还有如下特性:

  • 创建独立的Spring项目
  • 内置Tomcat, Jetty,Undertow
  • 初始POM配置文件以简化Maven配置
  • 尽可能的自动配置Spring
  • 提供生产环境功能,如统计,健康检查和外部配置
  • 无需XML配置和代码生成
  • 开发环境:IntelliJ, JDK 1.8
  • 项目源代码 Gitee

首先在IntelliJ中创建一个maven项目:

  • GroupID: cn.zxuqian
  • ArtifactId: helloworld

创建完成后IntelliJ右下角会提示自动导入Maven配置,选择Enable Auto-Import来启动自动导入。然后在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>cn.zxuqian</groupId>
    <artifactId>helloworld</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

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


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

<dependencies> 标签添加了spring-boot-starter-web依赖,即 Spring MVC 和相关运行时环境。spring-boot-maven-plugin插件提供了一组maven运行目标,可以方便的打包,部署和运行应用。稍等片刻Maven自动下载依赖后就可以上手写代码了。

创建第一个控制器

在 src/main 下新建一个包 cn.zxuqian.controllers 并在其中新建一个类,名为 HelloController 并添加如下代码:

package cn.zxuqian.controllers;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/")
    public String index() {
        return "Hello World!";
    }
    
}

  • @RestController 标记此类为 Rest 控制器,并准备好处理 Rest 请求。
  • @RequestMapping("/") 即此方法处理根路径请求,如 http://localhost:8080/。
  • index 方法返回 String 类型,即响应返回的是字符串数据,这里是 "Hello World"。

创建 Application 类

在 cn.zxuqian 包下创建 Application 类,并添加如下代码:

package cn.zxuqian;

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

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
  • @SpringBootApplication 标明了此类为 Spring Boot 应用的启动类。

运行应用

在IntelliJ的右侧选项卡中选择 Maven Projects,然后展开 Plugins-->spring-boot,选择 spring-boot:run 目标。待启动成功后,在浏览器中访问 http://localhost:8080 看到 Hello World! 即为成功。

文章出自我的博客:http://zxuqian.cn/spring-boot-get-started/,欢迎访问。