【SpringBoot框架】起步-通過Intellij idea搭建Spring Boot的Reactive Web應用

第一步:打開Intellij Idea工具並進入File - New - Project,選擇Spring initializr項。

如果Spring Initializr項沒有找到,可以通過File - Settings - Plugins的Installed中查找下有沒有Spring Boot這個插件的安裝後的狀態,如果沒有啓用,勾選啓用就解決了;如果沒有安裝可以通過Marketplace選項卡中搜索並安裝解決。

第二步:通過以上默認設置,點擊next進入項目的結構信息的配置頁。

第三步:設置完成後,點擊next進入應用依賴包的選擇頁。

這裏只選擇了最基礎的Developer Tools下的Lombok包和Web下的Spring Reactive Web包。

第四步:選擇完成後,點擊Next進入項目位置的設置頁,設置完成後點擊Finish完成項目的初始化。

通過以上配置Intellij Idea會自動生成項目的核心文件,進入項目工作空間後,可能需要配置Maven的相關配置,可以進入File - Settings - Maven。

項目的結構如下:

第五步:新建HelloController類,驗證搭建結果。

package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping(value = "/demo/", method = RequestMethod.GET)
public class HelloController {
    @GetMapping("hello")
    public Mono<String> mono() {
        return Mono.just("hello world");
    }
} 

 第六步:右鍵DemoApplication啓動類,運行項目。

 啓動完成後,瀏覽器輸入http://localhost:8080/demo/hello,驗證結果。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章