關注「Java後端技術全棧」php
回覆「面試」獲取全套大廠面試資料java
快速構建Spring Boot基礎項目的兩種方式:web
spring官方工具面試
IDEA工具spring
spring官方工具構建Spring Boot項目
瀏覽器上輸入地址:https://start.spring.io/apache
如圖所示後端
我這裏使用的是maven,語言選擇java。packaging選擇Jar,JDK版本選擇本身的版本,我這裏選擇的JDK1.8。
瀏覽器
最後選擇一個SpringBoot版本。app
右邊是添加依賴maven
這裏咱們選個spring web,下載到本地
解壓到本身的本地空間,而後使用IDEA導入,項目目錄:
pom.xml
<?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.3.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
DemoApplication.java
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
項目其餘都爲空,application.properties都是空白的。
爲了演示,這裏添加一個Controller
package com.example.demo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @GetMapping("/hello") public String hello(){ return "hello world" ; } }
啓動DemoApplication.java中的main函數。
默認端口是8080。
在瀏覽器上輸入:http://localhost:8080/hello
以上即是使用spring官方提供的工具建立Spring Boot項目的一種方式。下面來使用IDEA建立Spring Boot項目。
IDEA建立Spring Boot項目
Intellij IDEA 中的 Spring Initializr 工具
File-->new---project
選擇next,咱們能夠看到
Group 顧名思義就是你的公司名,通常是填寫com.***。
Artifact groupId 和 artifactId 是maven管理項目包時用做區分的字段,就像是地圖上的座標。這裏填寫項目名便可。
Type 就是構建的項目類型,意思就是你但願你的項目使用什麼工具構建,可選 maven 和 gradle 通常選 maven。
Language 顧名思義就是你的項目用啥語言開發,可選 Java、Groovy、Kotlin
Packaging 就是你但願你的項目打成什麼形式的包,可選 Jar、War SpringBoot 項目通常選 Jar
Java Version 意指項目使用的 java 版本,根據你的須要選擇,我這裏使用的是8。
Version 項目的初始版本,默認便可。
Name 項目名稱。
Description 項目描述,默認便可。
Package 包名,填完 Group 和 Artifact 後自動生成,默認便可。
繼續 next
這裏爲了好演示,因此選擇web,這裏是能夠選擇不少的,有空自行翻閱一下,一鍵式搞定依賴。也稱約定大於配置。
next(由於前面已經有demo,因此這裏取個demo1)
Finish.
導入到IDEA中
和前面使用spring官方工具構建的一毛同樣。
而後繼續把前面那個Controller搞過來
啓動DemoApplication
瀏覽器上輸入:http://localhost:8080/hello
好了,以上即是今天分享的兩週構建spring boot項目的方法。但願上面兩種方式對你有所幫助。
碼字不易,點個在看+分享,很是感謝!
本文分享 CSDN - 田維常。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。