日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

第一个SpringBoot入门级项目(超详细步骤)

發(fā)布時間:2025/3/19 javascript 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第一个SpringBoot入门级项目(超详细步骤) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

開發(fā)環(huán)境:

????????????????MyEcplise2017+Maven+SpringBoot+Tomcat 8.5

① 創(chuàng)建一個新的Maven項目:File-->New-->Others-->Maven Project

?

?

?

?

?

?

?

?

?

?

?

?

?

② 點擊next,選擇項目路徑:

? ? create a simple project(skip archetype selection):創(chuàng)建一個簡單項目(跳過原型選擇)

? ? 如果選擇,則跳過Maven的項目原型,建議不勾選

? ? Use default Workspace location:使用默認(rèn)的工作區(qū)間

? ? Add project(s) to working set:添加到工作集,選中就會將項目歸類,可選可不選

③ 單擊next,打開對話框

?????選擇項目類型:

????????通常選擇maven-archetype-quickstart(非web項目)項目模型

????????或者maven-archetype-webapp(Web項目)項目模型,這兩種比較常用

??④ 點擊next,打開對話框

填寫項目參數(shù):

GroupId:項目組織的唯一的標(biāo)識符

ArtifactId:項目的名稱,這里寫hellospringboot

Version:當(dāng)前版本,會自動填補

Package:默認(rèn)包結(jié)構(gòu),會自動填補

⑤ 單擊finish,第一個項目完成,完成后的項目目錄為

src/main/java:這個目錄存儲主要的Java代碼

src/test/java:存儲測試用的類,比如Junit的測試

⑤修改pom.xml

pom.xml是Maven的基礎(chǔ)配置文件,修改pom.xml文件,先打開自動生成的pom.xml

<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>com.ysh</groupId><artifactId>hellospringboot</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>hellospringboot</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies> </project>

?

一、在url元素后面添加parent元素信息:

? <!-- ?? spring-boot-starter-parent是Spring Boot的核心啟動器, 包含了自動配置、日志和YAML等大量默認(rèn)的配置,大大簡化了我們的開發(fā)。 引入之后相關(guān)的starter引入就不需要添加version配置,spring boot會自動選擇最合適的版本進行添加。--><parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath/>?</parent>

二、在dependencies元素中添加dependency元素,添加之后右擊項目選中Maven,再選擇update project

? <!-- spring-boot-starter-web包含了Spring Boot預(yù)定義的一些Web開發(fā)的常用依賴包 如: spring-webmvc,Tomcat.... --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>

?

修改之后的pom.xml:

<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>com.ysh</groupId><artifactId>hellospringboot</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>hellospringboot</name><url>http://maven.apache.org</url><!-- ?? spring-boot-starter-parent是Spring Boot的核心啟動器, 包含了自動配置、日志和YAML等大量默認(rèn)的配置,大大簡化了我們的開發(fā)。 引入之后相關(guān)的starter引入就不需要添加version配置,spring boot會自動選擇最合適的版本進行添加。--><parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath/>?</parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- spring-boot-starter-web包含了Spring Boot預(yù)定義的一些Web開發(fā)的常用依賴包 如: spring-webmvc,Tomcat.... --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency></dependencies></project>

?

⑥ 編寫測試代碼

寫一個java類HelloController,位于src/main/java下的com.ysh.hellospringboot下

package com.ysh.hellospringboot;import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;//RestController相當(dāng)于SpringMVC中的 @Controller + @ResponseBody @RestController public class HelloController { // 映射"/hello"請求 @RequestMapping("/hello") public String hello(){ return "Hello Spring Boot!"; }}

⑦修改Maven默認(rèn)的App類

package com.ysh.hellospringboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;/*** Hello world!**/ //此注解指定這是一個SpringBoot的應(yīng)用程序,不加就會報異常 Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean @SpringBootApplication public class App? {public static void main( String[] args ){//SpringApplication用于從main方法中啟動Spring應(yīng)用的類SpringApplication.run(App.class, args);} } ?

⑧ 啟動SpringBoot項目,如何啟動請參照:https://blog.csdn.net/badao_liumang_qizhi/article/details/80948956

⑨ 在瀏覽器中輸入URL來測試應(yīng)用

http://localhost:8080/hello

SpringBoot項目啟動后,默認(rèn)訪問地址為http://localhost:8080/,按照之前的Web項目習(xí)慣,怎么沒有項目路徑,SpringBoot會將項目路徑直接設(shè)為跟路徑

⑩ 第一次運行時報錯:

Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

原因是App類上沒加注解:

//此注解指定這是一個SpringBoot的應(yīng)用程序,不加就會報異常 Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean
@SpringBootApplication

public class App?

?

附源碼:https://download.csdn.net/download/badao_liumang_qizhi/10526582

?

?

總結(jié)

以上是生活随笔為你收集整理的第一个SpringBoot入门级项目(超详细步骤)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。