需求
使用SpringBoot開發一個web應用,瀏覽器發起請求/hello后,給瀏覽器返回字符串 hello world~
步驟
1.創建Maven工程
2.導入spring-boot-stater-web起步依賴
3.編寫controller
4.提供啟動類
?
pom.xml文件了解
啟動類
新建包
創建類
package com.zwh.springbootquickstart.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {@RequestMapping("/hello")public String hello() {return "Hello World";}
}
運行
端口號
結果
遇到的問題
問題1
問題2
問題3
問題出在Maven嘗試從中央倉庫(https://repo.maven.apache.org/maven2?)下載commons-io:commons-io:jar:2.11.0
時失敗,并且這個失敗被緩存到了本地倉庫,導致后續的構建過程中不再嘗試重新下載該依賴
解決
添加鏡像使其需要的包完整下載
<!-- 配置阿里云倉庫 -->
<repositories><repository><id>aliyun-repos</id><url>https://maven.aliyun.com/repository/public</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></repository>
</repositories>
<pluginRepositories><pluginRepository><id>aliyun-repos</id><url>https://maven.aliyun.com/repository/public</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></pluginRepository>
</pluginRepositories>
問題4
警告信息指出,在未命名模塊中調用了受限方法java.lang.System::load
,并且建議使用--enable-native-access=ALL-UNNAMED
參數來避免警告。如果不啟用原生訪問,未來版本中這些受限方法將被阻止。
給當前工程添加
--enable-native-access=ALL-UNNAMED--add-opens java.base/java.lang=ALL-UNNAMED
給模版添加,為了方便之后使用