当前位置: 首页 > article >正文

创建SpringBoot Helloword 程序详细步骤

本文档实现SpringBoot hello word 程序,翻译于Spring | Quickstart

目录

  • 一、项目创建步骤
    • 1.1 创建项目
    • 1.2 添加代码
    • 1.3 运行
  • 参考教程

一、项目创建步骤

1.1 创建项目

在官网Spring Initializr上创建项目

请添加图片描述

1.2 添加代码

在IDE中打开项目并在src/main/java/com/zouhu/helloword文件夹中找到HellowordApplication.java文件,现在通过添加下面代码中显示的额外方法和注释来更改文件的内容。

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {
    public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
    }
    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
      return String.format("Hello %s!", name);
    }
}

hello()方法被设计为采用一个名为name的String参数,然后将该参数与代码中的单词“Hello”结合起来。这意味着如果您在请求中将姓名设置为“Amy”,则响应将是“Hello Amy”。

@RestController注释告诉Spring,这段代码描述了一个应该在Web上可用的端点。

@GetMap(“/hello”)告诉Spring使用我们的hello()方法来回答发送到http://localhost:8080/hello地址的请求。

最后,@RequestParam告诉Spring期望请求中有一个名称值,但如果不存在,它将默认使用单词“World”

1.3 运行

编译并运行程序。

根据启动界面可以看到以下信息:Spring Boot的嵌入式Apache Tomcat服务器充当网络服务器,并在localhost端口8080上侦听请求。

请添加图片描述

打开浏览器,在顶部的地址栏中输入http://localhost:8080/hello即可访问

也可以通过添加参数来访问http://localhost:8080/hello?name=zouhu

请添加图片描述

参考教程

Spring | Quickstart


http://www.kler.cn/a/153052.html

相关文章:

  • 【C++ Primer Plus学习记录】嵌套循环和二维数组
  • 营销系统规则引擎
  • uni-app+ts----微信小程序锚点定位 、自动吸顶、滚动自动选择对应的锚点(点击tab跳转对应的元素位置)
  • 基于Spring、SpringMVC、MyBatis的外卖点餐网站
  • 【数据结构】初识排序 直接插入排序
  • Google Analytics(谷歌分析)是什么以及如何使用
  • 电商营销场景的RocketMQ实战01-RocketMQ原理
  • C语言贪吃蛇(有详细注释)
  • 四个方法,设置excel文件只读模式
  • C++相关闲碎记录(3)
  • 模糊C均值(Fuzzy C-means,FCM)聚类的可运行的python程序代码,复制即可用!!切记需要安装库 scikit-fuzzy
  • MFC、VC++操作excel后,excel程序进程无法正常退出的非暴力处理方法
  • 【Java SE】泛型
  • 高德Map
  • 基于web的舞蹈选课管理系统设计与实现
  • 反转链表的Java实现
  • Elasticsearch(ES)概述
  • 【python学习】Python 线程编程(类内多线程编程)
  • 分治算法——75. 颜色分类
  • Leetcode 2948. Make Lexicographically Smallest Array by Swapping Elements