App下載

如何創(chuàng)建一個(gè)SpringBoot項(xiàng)目?——springboot的helloworld項(xiàng)目

猿友 2021-06-19 16:53:27 瀏覽數(shù) (2569)
反饋

很多小伙伴在學(xué)習(xí)完java后端后就開始了解java的幾個(gè)框架,其中最出名的應(yīng)該屬于Spring的框架。而springboot作為spring自家產(chǎn)品,有些小伙伴也開始對其產(chǎn)生興趣。今天小編就以傳統(tǒng)的helloworld為例,來介紹一下如何創(chuàng)建一個(gè)SpringBoot項(xiàng)目吧。

閱前須知

springboot需要maven或者gradle來進(jìn)行項(xiàng)目管理,小編接下來以maven為例。如果沒有maven的基礎(chǔ),可以前往maven教程進(jìn)行學(xué)習(xí)。

創(chuàng)建空的springboot項(xiàng)目

前往官網(wǎng)進(jìn)行創(chuàng)建,網(wǎng)站如下:

https://start.spring.io/

項(xiàng)目我們選擇maven項(xiàng)目,語言選擇java,springboot版本按自己需求進(jìn)行選擇,小編這里選擇2.5.1版。項(xiàng)目信息按自身情況進(jìn)行填寫,選擇自己需要的打包方式和對應(yīng)的java版本(前面這些都可以按自身需求進(jìn)行填寫)。

依賴我們選擇spring web和devtools就可以,后期依賴添加可以通過maven進(jìn)行管理。

springboot創(chuàng)建

點(diǎn)擊create,該網(wǎng)站就會(huì)生成一個(gè)對應(yīng)的項(xiàng)目的壓縮包,下載完成后解壓就能打開一個(gè)空的項(xiàng)目了。

如果有maven創(chuàng)建項(xiàng)目的經(jīng)驗(yàn),也可以通過?pom.xml?來修改自己的項(xiàng)目使其變成springboot項(xiàng)目(以下是上面項(xiàng)目對應(yīng)的?pom.xml?,可以自行參考后進(jìn)行修改)。

<?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>2.5.1</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>W3Cschool</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>W3Cschool</name>
	<description>618最后返場三天,確定不來看看嗎</description>
	<properties>
		<java.version>11</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-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</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>

項(xiàng)目結(jié)構(gòu)大致如下:

項(xiàng)目結(jié)構(gòu)

創(chuàng)建HelloController

在?W3CschoolApplication.java?的同級文件夾下新建一個(gè)?HelloController.java?,然后輸入如下代碼(代碼注釋如下):

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

@RestController//注解,接下來的代碼是一個(gè)控制器
@RequestMapping("/test")//一級路徑為/test的時(shí)候執(zhí)行下面這個(gè)類
public class HelloController {
    @RequestMapping("/hello")//二級路徑為/hello的時(shí)候執(zhí)行下面這個(gè)方法
    public String hello(){
        return "helloworld";//返回helloworld
    }
}

運(yùn)行

springboot內(nèi)嵌了Tomcat模塊,所以它可以向java一樣運(yùn)行(它有?main()?方法),運(yùn)行?W3CschoolApplication.java即可啟動(dòng)這個(gè)項(xiàng)目。

啟動(dòng)后在瀏覽器訪問http://localhost:8080/test/hello即可查看項(xiàng)目的運(yùn)行效果。

運(yùn)行結(jié)果

小結(jié)

以上就是springboot helloworld示例。更多springboot的內(nèi)容學(xué)習(xí)還請前往springboot微課。


0 人點(diǎn)贊