SpringBoot教程_創(chuàng)建第一個(gè)SpringBoot項(xiàng)目
創(chuàng)建SpringBoot項(xiàng)目可以通過兩種方式
1、通過訪問:https://start.spring.io/,SpringBoot的官方網(wǎng)站進(jìn)行創(chuàng)建SpringBoot項(xiàng)目;
2、通過工具(例如:Idea)創(chuàng)建SpringBoot項(xiàng)目。本次使用IDEA創(chuàng)建第一個(gè)SpringBoot項(xiàng)目。
首先,打開我們的Idea開發(fā)工具

選擇Create New Poject(創(chuàng)建一個(gè)新的項(xiàng)目)

我們使用Spring Initializr創(chuàng)建SpringBoot項(xiàng)目
SDK默認(rèn)選擇你安裝的JDK路徑,默認(rèn)選擇SpringBoot官網(wǎng)地址,點(diǎn)擊Next

在這里填寫你的包名和項(xiàng)目名
我們使用Maven Project,選擇項(xiàng)目打包方式為:jar,Java版本為8,默認(rèn)就好,點(diǎn)擊Next

然后選擇我們需要使用到的Maven依賴
這里我只選擇了Web項(xiàng)目開發(fā)的依賴,當(dāng)然,你也可以選擇更多的依賴,例如:MySQL,jpa,lombok,還有SpringCloud的一些東西,根據(jù)自己需要來選擇,點(diǎn)擊Next

這里是讓你填寫你的項(xiàng)目名稱和項(xiàng)目地址的信息
點(diǎn)擊Finish,我們就完成了第一個(gè)SpringBoot項(xiàng)目的創(chuàng)建工作,接下來只需要等待Idea成功引入Maven依賴,項(xiàng)目就算創(chuàng)建完成

SpringBoot項(xiàng)目創(chuàng)建完成之后的目錄結(jié)構(gòu)就是這個(gè)樣子
.mvn,mvnw,mvnw.cmd這三個(gè)文件是可以刪除的,TestApplication.java是我們這個(gè)項(xiàng)目的啟動(dòng)文件,我們?cè)趩?dòng)項(xiàng)目的時(shí)候,只需要Run這個(gè)文件或者Debug Run這個(gè)文件就可以了,接下來我們看看pom.xml文件的內(nèi)容
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.test</groupId> <artifactId>test</artifactId> <version>0.0.1-SNAPSHOT</version> <name>test</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</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>
我們?cè)谶x擇依賴的時(shí)候
選擇了web的依賴,已經(jīng)成功引入,另外還默認(rèn)引入了測試的依賴,接下來就讓我們來啟動(dòng)它吧
package com.test.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
@RequestMapping(value = "/init")
public String init(@RequestParam("name") String name) {
return "Hello," + name + "!";
}
}在啟動(dòng)之前,我們?cè)陧?xiàng)目的啟動(dòng)文件上加了@RestController注解,它是@Controller和@ResponseBody的結(jié)合,寫上@RestController注解,就相當(dāng)于把后兩者都給寫上了,并寫了一個(gè)地址為“init”方法,接收一個(gè)“name”參數(shù),返回一個(gè)String類型的數(shù)據(jù),然后我們啟動(dòng)項(xiàng)目

我們啟動(dòng)后會(huì)看到SpringBoot的字樣,然后在啟動(dòng)信息中會(huì)看到啟動(dòng)在本地的8080端口,以及啟動(dòng)費(fèi)時(shí)等信息,然后我們打開瀏覽器訪問地址:http://localhost:8080/init?name=World
頁面顯示信息:Hello,World!,代表我們的項(xiàng)目啟動(dòng)成功,并能夠成功訪問!
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java?Chassis3的多種序列化方式支持技術(shù)解密
這篇文章主要為大家介紹了Java?Chassis?3多種序列化方式支持技術(shù)解密,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Java 1,2,3,4能組成多少個(gè)互不相同且無重復(fù)數(shù)字的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java 1,2,3,4能組成多少個(gè)互不相同且無重復(fù)數(shù)字的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-02-02
Java面試官最喜歡問的關(guān)鍵字之volatile詳解
這篇文章主要給大家介紹了關(guān)于Java面試官最喜歡問的關(guān)鍵字之volatile的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
java Socket編程實(shí)現(xiàn)I/O多路復(fù)用的示例
本文主要介紹了java Socket編程實(shí)現(xiàn)I/O多路復(fù)用的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09
Spring中ApplicationListener的使用解析
這篇文章主要介紹了Spring中ApplicationListener的使用解析,ApplicationContext事件機(jī)制是觀察者設(shè)計(jì)模式的實(shí)現(xiàn),通過ApplicationEvent類和ApplicationListener接口,需要的朋友可以參考下2023-12-12
Java通過socket客戶端保持連接服務(wù)端實(shí)現(xiàn)代碼
這篇文章主要介紹了Java通過socket客戶端保持連接服務(wù)端實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11

