怎么快速搭建一個(gè)springboot項(xiàng)目-創(chuàng)新互聯(lián)

怎么快速搭建一個(gè)springboot項(xiàng)目?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

成都創(chuàng)新互聯(lián)公司專注于德宏州網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供德宏州營(yíng)銷型網(wǎng)站建設(shè),德宏州網(wǎng)站制作、德宏州網(wǎng)頁(yè)設(shè)計(jì)、德宏州網(wǎng)站官網(wǎng)定制、重慶小程序開發(fā)服務(wù),打造德宏州網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供德宏州網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

1. 問題描述

springboot的面世,成為Java開發(fā)者的一大福音,大大提升了開發(fā)的效率,其實(shí)springboot只是在maven的基礎(chǔ)上,對(duì)已有的maven gav進(jìn)行了封裝而已,今天用最簡(jiǎn)單的代碼快速入門springboot。

2. 解決方案

強(qiáng)烈推薦大家使用Idea的付費(fèi)版(破解感謝下藍(lán)宇),Idea對(duì)maven、git等插件支持的更加好。

使用idea自帶的spring Initializr(實(shí)際調(diào)用的是springboot的官網(wǎng)上的initializr),快速新建springboot項(xiàng)目。

2.1 新建Springboot項(xiàng)目

(1)file->new->project

怎么快速搭建一個(gè)springboot項(xiàng)目

(2)點(diǎn)擊next(第一個(gè))

創(chuàng)建springboot項(xiàng)目(因?yàn)檫B接的國(guó)外的網(wǎng)站,next有時(shí)會(huì)幾秒的延遲),將兩個(gè)值改成自己的配置,Group:com.laowang ,Artifact:sptest,其他可以不用動(dòng),點(diǎn)擊ok

怎么快速搭建一個(gè)springboot項(xiàng)目

(3)點(diǎn)擊next(第二個(gè))

選擇web-》spring web starter

怎么快速搭建一個(gè)springboot項(xiàng)目

(4)點(diǎn)擊next(第三個(gè))

不用做修改,直接finish

怎么快速搭建一個(gè)springboot項(xiàng)目

新建springboot項(xiàng)目已經(jīng)完成。

2.2 springboot默認(rèn)生成三個(gè)文件

默認(rèn)生成的三個(gè)文件

2.2.1. 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 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.4.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.laowang</groupId>
 <artifactId>sptest</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>sptest</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>

重點(diǎn)就一個(gè)gav:spring-boot-starter-web,其他可以刪除。

2.2.2 application.properties

該文件默認(rèn)為空,springboot的默認(rèn)啟動(dòng)端口號(hào):8080,可以在改文件修改。

2.2.3 啟動(dòng)類文件(SptestApplication.java)

package com.laowang.sptest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SptestApplication {

 public static void main(String[] args) {
  SpringApplication.run(SptestApplication.class, args);
 }

}

重點(diǎn)是標(biāo)簽:@SpringBootApplication

2.3 驗(yàn)證springboot

在com.laowang.sptest報(bào)下新建ctroller包,并新建類:HelloController

package com.laowang.sptest.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

@Controller
public class HelloController {

 @RequestMapping("/")
 @ResponseBody
 public String getHello() {
  return "hello";
 }
}

執(zhí)行效果:

怎么快速搭建一個(gè)springboot項(xiàng)目

服務(wù)正常啟動(dòng)。

2.4 重點(diǎn)說明

需要說明兩點(diǎn):

(1)類文件要放在跟啟動(dòng)類同級(jí)或者下一目錄下,本項(xiàng)目為:com.laowang.sptest包下面。因?yàn)閟pringboot默認(rèn)掃描加載啟動(dòng)類同級(jí)或者下級(jí)目錄的標(biāo)簽類(@RestController,@Service ,@Configuraion,@Component等),假如真需要加載其他目錄的標(biāo)簽類,可以通過在啟動(dòng)上配置標(biāo)簽@ComponentScan(具體包)來(lái)進(jìn)行掃描加載。

(2)資源文件默認(rèn)放到resources下面,templates默認(rèn)放的是動(dòng)態(tài)文件,比如:html文件,不過要搭配thymeleaf 使用(pom文件中需新加gav)。

其他也沒什么了,springboot主要是通過spring提供的多個(gè)starter和一些默認(rèn)約定,實(shí)現(xiàn)項(xiàng)目的快速搭建。

關(guān)于怎么快速搭建一個(gè)springboot項(xiàng)目問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

文章標(biāo)題:怎么快速搭建一個(gè)springboot項(xiàng)目-創(chuàng)新互聯(lián)
瀏覽地址:http://bm7419.com/article12/dscogc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、小程序開發(fā)品牌網(wǎng)站建設(shè)網(wǎng)頁(yè)設(shè)計(jì)公司、自適應(yīng)網(wǎng)站網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化