JAVA Spring Boot快速开始
实践环境
Spring Boot 3.2.1
Maven 3.8.8
JDK 1.8.0_331
创建项目
通过http://start.spring.io/网站创建包含Spring Boot的项目,具体如下:

点击 GENERATE 按钮后,会自动生成并下载 SpringBootQuickStartDemo.zip
导入项目
解压述下载的项目压缩包,解压后的项目文件结构如下:
E:codeProjects\SpringBootQuickStartDemo>tree /f
.
│ .gitignore
│ HELP.md
│ mvnw
│ mvnw.cmd
│ pom.xml
│
├─.mvn
│ └─wrapper
│ maven-wrapper.jar
│ maven-wrapper.properties
│
└─src
├─main
│ ├─java
│ │ └─org
│ │ └─example
│ │ └─SpringBootQuickStartDemo
│ │ SpringBootQuickStartDemoApplication.java
│ │
│ └─resources
│ │ application.properties
│ │
│ ├─static
│ └─templates
└─test
└─java
└─org
└─example
└─SpringBootQuickStartDemo
SpringBootQuickStartDemoApplicationTests.java
其中,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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>SpringBootQuickStartDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootQuickStartDemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
注意:不同版本的Spring Boot对JAVA JDK有不同的要求,所以需要根据实际配置考虑是否修改上述pom.xml,具体有哪些系统要求,可以参考以下链接
https://docs.spring.io/spring-boot/docs/{SpringBootVersion}/reference/html/getting-started.html#getting-started.system-requirements
访问上述链接之前,修改 {SpringBootVersion}为具体版本号,比如 2.7.9
修改Spring Boot版本为2.7.9
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.9</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
修改JAVA版本
<java.version>8</java.version>
说明:笔者本机安装JDK1.8,如果不修改pom.xml配置,运行时会报类似如下错误:
Error:(2, 32) java: 无法访问org.springframework.boot.SpringApplication
错误的类文件: /D:/maven-repo/org/springframework/boot/spring-boot/3.2.1/spring-boot-3.2.1.jar!/org/springframework/boot/SpringApplication.class
类文件具有错误的版本 61.0, 应为 52.0
请删除该文件或确保该文件位于正确的类路径子目录中。
接着,使用IDEA打开该项目
添加代码
修改SpringBootQuickStartDemoApplication.java,该文件默认生成的内容如下
package org.example.SpringBootQuickStartDemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootQuickStartDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootQuickStartDemoApplication.class, args);
}
}
修改文件内容为如下:
package org.example.SpringBootQuickStartDemo;
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 SpringBootQuickStartDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootQuickStartDemoApplication.class, args);
}
@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
}
这就是在Spring Boot中创建一个简单的“Hello World”web服务所需的所有代码。
添加的hello()方法接收一个名为name的String参数,返回"Hello " 与name参数的字符串拼接。这意味着,如果在请求中将name参数值设置为“Amy”,则响应将为“Hello Amy”。
@RestController注释告诉Spring,这段代码描述了一个应该可通过web访问的端点(endpoint)。@GetMapping("/hello")告诉Spring使用我们的hello()方法来响应访问http://localhost:8080/hello的请求。最后@RequestParam告诉Spring请求需要提供一个name值,如果未提供的话,它将默认使用单词World。
测试
IDEA中打开SpringBootQuickStartDemoApplication.java文件,右键 -> Run 'SpringBookQuic....main()' ,控制台输出类似如下内容:
...略
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.9)
2024-01-02 00:14:28.051 INFO 19408 --- [ main] .e.S.SpringBootQuickStartDemoApplication : Starting SpringBootQuickStartDemoApplication using Java 1.8.0_331 on SF0001420551A with PID 19408 (E:\codeProjects\SpringBootQuickStartDemo\target\classes started by 01367599 in E:\codeProjects\SpringBootQuickStartDemo)
2024-01-02 00:14:28.055 INFO 19408 --- [ main] .e.S.SpringBootQuickStartDemoApplication : No active profile set, falling back to 1 default profile: "default"
2024-01-02 00:14:29.778 INFO 19408 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2024-01-02 00:14:29.792 INFO 19408 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2024-01-02 00:14:29.792 INFO 19408 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.71]
2024-01-02 00:14:30.146 INFO 19408 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2024-01-02 00:14:30.146 INFO 19408 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2034 ms
2024-01-02 00:14:30.573 INFO 19408 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2024-01-02 00:14:30.583 INFO 19408 --- [ main] .e.S.SpringBootQuickStartDemoApplication : Started SpringBootQuickStartDemoApplication in 3.071 seconds (JVM running for 3.784)
最后两行告诉我们Spring Boot已启动。Spring Boot的内置Apache Tomcat服务器充当Web服务器,监听本地8080端口。
浏览地址栏中输入并访问http://localhost:8080/hello

输入并访问http://localhost:8080/hello?name=shouke

参考连接
https://docs.spring.io/spring-boot/docs/
JAVA Spring Boot快速开始的更多相关文章
- 使用 Spring Boot 快速构建 Spring 框架应用--转
原文地址:https://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/ Spring 框架对于很多 Java 开发人员来说都不陌生.自从 2 ...
- Spring Boot 快速入门
Spring Boot 快速入门 http://blog.csdn.net/xiaoyu411502/article/details/47864969 今天给大家介绍一下Spring Boot MVC ...
- 使用 Spring Boot 快速构建 Spring 框架应用,PropertyPlaceholderConfigurer
Spring 框架对于很多 Java 开发人员来说都不陌生.自从 2002 年发布以来,Spring 框架已经成为企业应用开发领域非常流行的基础框架.有大量的企业应用基于 Spring 框架来开发.S ...
- Spring Boot快速入门(二):http请求
原文地址:https://lierabbit.cn/articles/4 一.准备 postman:一个接口测试工具 创建一个新工程 选择web 不会的请看Spring Boot快速入门(一):Hel ...
- spring boot入门教程——Spring Boot快速入门指南
Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将使创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运 ...
- Spring Boot 快速入门(IDEA)
从字面理解,Boot是引导的意思,因此SpringBoot帮助开发者快速搭建Spring框架:SpringBoot帮助开发者快速启动一个Web容器:SpringBoot继承了原有Spring框架的优秀 ...
- Spring Boot 快速入门 史上最简单
1.Spring Boot 概述 Spring Boot 是所有基于 Spring 开发的项目的起点.Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的 ...
- Spring Boot快速集成kaptcha生成验证码
Kaptcha是一个非常实用的验证码生成工具,可以通过配置生成多样化的验证码,以图片的形式显示,从而无法进行复制粘贴:下面将详细介绍下Spring Boot快速集成kaptcha生成验证码的过程. 本 ...
- 笔记61 Spring Boot快速入门(一)
IDEA+Spring Boot快速搭建 一.IDEA创建项目 略 项目创建成功后在resources包下,属性文件application.properties中,把数据库连接属性加上,同时可以设置服 ...
- 10个Spring Boot快速开发的项目,接私活利器(快速、高效)
本文为大家精选了 码云 上优秀的 Spring Boot 语言开源项目,涵盖了企业级系统框架.文件文档系统.秒杀系统.微服务化系统.后台管理系统等,希望能够给大家带来一点帮助:) 1.项目名称:分布式 ...
随机推荐
- phpstorm配置laravel语法提示
摘自:https://cloud.tencent.com/developer/article/1426699 phpstorm配置laravel语法提示 2019-05-15阅读 1930 用习惯 ...
- Swoole 源码分析之 Http Server 模块
首发原文链接:Swoole 源码分析之 Http Server 模块 Swoole 源码分析之 Http Server 模块 Http 模块的注册初始化 这次我们分析的就是 Swoole 官网的这段代 ...
- zabbix笔记_005 zabbix自动发现
自动发现 [消耗资源较大] 1.1 自动发现监控主机 自动发现的好处: 快速发现,并自动添加主机,省去管理员配置的麻烦. 管理简单高效 zabbix监控构建速度更高效 1.2 自动发现的原理 自动发现 ...
- 微信iOS消息拦截插件教程-手机越狱环境搭建
微信iOS消息拦截插件教程-手机越狱环境搭建 标签(空格分隔): ios越狱开发 环境 背景介绍 本教程所有内容免费 本教程来源于一次知识分享,如果有需要了解更多的 请联系QQ:480071411 i ...
- 判断是不是ie浏览器 加上ie11
var b_version = navigator.appVersion; var version = b_version.split(";"); var trim_Version ...
- C# .NET HttpWebRequest 显示指定SSL TLS 版本
C# .NET HttpWebRequest 显示指定SSL TLS 版本 (TLS1.0,TLS1.1,TLS1.2) 在程序启动时加入这段代码: ServicePointManager.Secur ...
- 开源的Datadog?可观测性平台SigNoz是否名副其实?
SigNoz号称自己是开源领域的Datadog,基于OpenTelemetry做了一套可观测性方案.夜莺从V6版本开始,也希望做全栈可观测性方案,巧了,大家目标一致,今天我们一起来对SigNoz做个初 ...
- Promise 期约
Promise 期约之前 回调地狱 设想这样一个经常发生的场景,我们希望处理Ajax请求的结果,所以我们将处理请求结果的方法作为回调传入,需要将请求结果继续处理,这就导致我们陷入了回调地狱 doSom ...
- JS 过滤掉两个数组中对象id值相等的项
const arr1 = [{ id: 1, name: '老二' }]; const arr2 = [{ id: 1, name: '网' }, { id: 2, name: '二位' },{ id ...
- Nuxt3页面开发实战探索
title: Nuxt3页面开发实战探索 date: 2024/6/19 updated: 2024/6/19 author: cmdragon excerpt: 摘要:这篇文章是关于Nuxt3页面开 ...