javascript
第一个SpringBoot入门级项目(超详细步骤)
開發環境:
????????????????MyEcplise2017+Maven+SpringBoot+Tomcat 8.5
① 創建一個新的Maven項目:File-->New-->Others-->Maven Project
?
?
?
?
?
?
?
?
?
?
?
?
?
② 點擊next,選擇項目路徑:
? ? create a simple project(skip archetype selection):創建一個簡單項目(跳過原型選擇)
? ? 如果選擇,則跳過Maven的項目原型,建議不勾選
? ? Use default Workspace location:使用默認的工作區間
? ? Add project(s) to working set:添加到工作集,選中就會將項目歸類,可選可不選
③ 單擊next,打開對話框
?????選擇項目類型:
????????通常選擇maven-archetype-quickstart(非web項目)項目模型
????????或者maven-archetype-webapp(Web項目)項目模型,這兩種比較常用
??④ 點擊next,打開對話框
填寫項目參數:
GroupId:項目組織的唯一的標識符
ArtifactId:項目的名稱,這里寫hellospringboot
Version:當前版本,會自動填補
Package:默認包結構,會自動填補
⑤ 單擊finish,第一個項目完成,完成后的項目目錄為
src/main/java:這個目錄存儲主要的Java代碼
src/test/java:存儲測試用的類,比如Junit的測試
⑤修改pom.xml
pom.xml是Maven的基礎配置文件,修改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等大量默認的配置,大大簡化了我們的開發。 引入之后相關的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預定義的一些Web開發的常用依賴包 如: 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等大量默認的配置,大大簡化了我們的開發。 引入之后相關的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預定義的一些Web開發的常用依賴包 如: 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相當于SpringMVC中的 @Controller + @ResponseBody @RestController public class HelloController { // 映射"/hello"請求 @RequestMapping("/hello") public String hello(){ return "Hello Spring Boot!"; }}⑦修改Maven默認的App類
package com.ysh.hellospringboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;/*** Hello world!**/ //此注解指定這是一個SpringBoot的應用程序,不加就會報異常 Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean @SpringBootApplication public class App? {public static void main( String[] args ){//SpringApplication用于從main方法中啟動Spring應用的類SpringApplication.run(App.class, args);} } ?⑧ 啟動SpringBoot項目,如何啟動請參照:https://blog.csdn.net/badao_liumang_qizhi/article/details/80948956
⑨ 在瀏覽器中輸入URL來測試應用
http://localhost:8080/hello
SpringBoot項目啟動后,默認訪問地址為http://localhost:8080/,按照之前的Web項目習慣,怎么沒有項目路徑,SpringBoot會將項目路徑直接設為跟路徑
⑩ 第一次運行時報錯:
Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
原因是App類上沒加注解:
//此注解指定這是一個SpringBoot的應用程序,不加就會報異常 Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean
@SpringBootApplication
public class App?
?
附源碼:https://download.csdn.net/download/badao_liumang_qizhi/10526582
?
?
總結
以上是生活随笔為你收集整理的第一个SpringBoot入门级项目(超详细步骤)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MyEcplise中SpringBoot
- 下一篇: spring boot创建应用 端口冲突