Spring Boot入門及第一個案例

一:SpringBoot是什麼

springboot是對spring的缺點進行改善和優化,約定大於配置  開箱即用  沒有代碼生成 也無需xml 文件配置   能夠修改屬性值來知足需求java

1) Spring Boot使編碼變簡單web

2) Spring Boot使配置變簡單spring

3) Spring Boot使部署變簡單apache

4) Spring Boot使監控變簡單瀏覽器

二:建立第一個SpringBoot工程

 一、點擊File--->New--->Project...

 

二、輸入MAVEN,組名、包名等相關參數

 

三、選擇SpringBoot版本,選擇項目須要依賴的相關骨架包

注意:有些版本此處顯示的是SpringWeb是同樣的springboot

 

四、設置項目保存目錄:

 

五、項目建立完成,工程主界面以下:

刪除多餘的這三個文件app

 

六、項目說明

(1)、默認有個Demo001Application類,裏面是spring boot的載入函數maven

(2)、resource目錄下有個application.properties文件,這個是Spring boot的配置文件函數

(3)、test目錄下有個測試類Demo001ApplicationTests,這個是spring boot的單元測試spring-boot

(4)、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.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.offcn</groupId>
<artifactId>firstdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>firstdemo</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>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
</project>

 

注意觀察

一個繼承spring-boot-starter-parent,兩個依賴,spring-boot-starter-web web項目依賴必須,spring-boot-starter-test spring boot項目單元測試依賴

 

注意,不少人配置的maven遠程倉庫地址,其中不少配置的阿里雲maven鏡像,在這會有找不到最新Springboot相關包的問題,請把遠程倉庫指向華爲雲:

<mirror>

    <id>huaweicloud</id>

    <mirrorOf>*</mirrorOf>

    <url>https://mirrors.huaweicloud.com/repository/maven/</url>

</mirror>

 

六、啓動項目

找到以下文字,代表SpringBoot已經成功啓動:

 

打開瀏覽器,輸入地址:http://localhost:8080 ,出現以下畫面

 

出現上圖404錯誤是正常的,由於咱們什麼都沒寫。

七、編寫HelloController

package com.offcn.demo.controller;

 

import org.springframework.stereotype.Controller;

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

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.ResponseBody;

 

@Controller

public class HelloController {

 

@RequestMapping(value="/hello",method=RequestMethod.GET)

@ResponseBody

public String sayHello() {

return "hello spring Boot!";

}

}

 

注意HelloController所在包,必須在com.offcn.demo包,或者子包下面。

重啓發現剛纔寫的hello已經映射出來了

訪問http://localhost:8080/hello

相關文章
相關標籤/搜索