Maven方式構(gòu)建SpringBoot項(xiàng)目的實(shí)現(xiàn)步驟(圖文)
一,創(chuàng)建Maven項(xiàng)目
創(chuàng)建項(xiàng)目 - HelloWorld01

單擊【Create】按鈕

二,添加依賴
在pom.xml文件里添加parent和web的起步器依賴

添加如下代碼:
<?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>net.army.boot</groupId>
<artifactId>HelloWorld01</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.11</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>刷新項(xiàng)目依賴

三,創(chuàng)建入口類
創(chuàng)建net.army.boot包,在包里創(chuàng)建啟動(dòng)類HelloWorld01Application

添加如下代碼:
package net.army.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 作者:梁辰興
* 日期:2023/5/23
* 功能:項(xiàng)目入口類
*/
@SpringBootApplication
public class HelloWorld01Application {
public static void main(String[] args) {
// 參數(shù)1:入口類示例;參數(shù)2:命令行參數(shù)
SpringApplication.run(HelloWorld01Application.class, args);
}
}注意:入口類必須添加注解符@SpringBootApplication,表明它是Spring Boot應(yīng)用。在主方法里,利用SpringApplication類的run()靜態(tài)方法啟動(dòng)HelloWorldApplication類的實(shí)例。
四,創(chuàng)建控制器
控制器是用于Web訪問的,在net.army.boot包里創(chuàng)建controller子包,然后在子包里創(chuàng)建控制器HelloController

添加如下代碼:
package net.army.boot.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 作者:梁辰興
* 日期:2023/5/23
* 功能:Hello控制器
*/
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "<h1 style='color: red; text-align: center'>Hello Spring Boot World~</h1>";
}
}注意:控制器添加注解符@RestController,該注解為組合注解,等同于Spring中@Controller+@ResponseBody注解。方法添加注解符@GetMapping(“/hello”),等同于Spring框架中@RequestMapping(RequestMethod.GET)注解。
五,運(yùn)行入口類
運(yùn)行入口類 - HelloWorld01Application

六,訪問Web頁面
在瀏覽器里訪問:http://localhost:8080/hello

七,修改訪問映射路徑
修改控制器HelloController

運(yùn)行入口類HelloWorld01Application,在瀏覽器里訪問:http://localhost:8080/hello

在瀏覽器里訪問:http://localhost:8080/lzy/hello

八,定制啟動(dòng)標(biāo)語
1、創(chuàng)建標(biāo)語文件
在resources目錄下創(chuàng)建banner.txt文件

2、生成標(biāo)語字符串
通過 http://patorjk.com/software/taag 網(wǎng)站生成標(biāo)語字符串

3、編輯標(biāo)語文件
將網(wǎng)站生成的標(biāo)語字符串復(fù)制到banner.txt文件里

4、查看啟動(dòng)標(biāo)語
啟動(dòng)應(yīng)用程序,查看啟動(dòng)標(biāo)語

5、關(guān)閉啟動(dòng)標(biāo)語
修改入口程序代碼

修改代碼如下:
package net.army.boot;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 作者:梁辰興
* 日期:2023/5/23
* 功能:項(xiàng)目入口類
*/
@SpringBootApplication
public class HelloWorld01Application {
public static void main(String[] args) {
// 創(chuàng)建Spring應(yīng)用
SpringApplication app = new SpringApplication(HelloWorld01Application.class);
// 設(shè)置標(biāo)語模式 - 關(guān)閉模式
app.setBannerMode(Banner.Mode.OFF);
// 運(yùn)行Spring應(yīng)用
app.run(args);
}
}啟動(dòng)應(yīng)用,查看效果,可以看到,項(xiàng)目啟動(dòng)標(biāo)語消失了

到此這篇關(guān)于Maven方式構(gòu)建SpringBoot項(xiàng)目的實(shí)現(xiàn)步驟(圖文)的文章就介紹到這了,更多相關(guān)Maven構(gòu)建SpringBoot內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot?基于?MongoTemplate?的工具類過程詳解
MongoDB是一個(gè)高性能,開源,無模式的文檔型數(shù)據(jù)庫(kù),是當(dāng)前NoSql數(shù)據(jù)庫(kù)中比較熱門的一種,這篇文章主要介紹了SpringBoot基于MongoTemplate的工具類,需要的朋友可以參考下2023-09-09
阿里通用OCR文字識(shí)別/圖像識(shí)別/圖片識(shí)別對(duì)接代碼示例(Java篇)
這篇文章主要介紹了阿里通用OCR文字識(shí)別/圖像識(shí)別/圖片識(shí)別對(duì)接(Java篇)的相關(guān)資料,文中詳細(xì)介紹了包括開通服務(wù)、測(cè)試圖片、編寫識(shí)別代碼、處理識(shí)別結(jié)果等步驟,需要的朋友可以參考下2024-12-12
kotlin中const 和val的區(qū)別及使用場(chǎng)景分析
在 Kotlin 中,const 和 val 都是用來聲明常量的,但它們的使用場(chǎng)景和功能有所不同,下面給大家介紹kotlin中const 和val的區(qū)別,感興趣的朋友一起看看吧2025-04-04
SpringBoot項(xiàng)目中resources文件讀取實(shí)踐
本文詳細(xì)介紹了SpringBoot項(xiàng)目中讀取resources目錄下文件的9種主流方式,并提供了一個(gè)完整的控制器Demo示例,幫助開發(fā)者快速定位最適合的資源加載方案2026-01-01
springboot項(xiàng)目啟動(dòng)自動(dòng)跳轉(zhuǎn)到瀏覽器的操作代碼
這篇文章主要介紹了springboot項(xiàng)目啟動(dòng)自動(dòng)跳轉(zhuǎn)到瀏覽器的操作代碼,本文圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03
mybatis對(duì)于list更新sql語句的寫法說明
這篇文章主要介紹了mybatis對(duì)于list更新sql語句的寫法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08

