首頁 > Java > java教程 > 如何在Intellij中運行第一個春季啟動應用程序?

如何在Intellij中運行第一個春季啟動應用程序?

Mary-Kate Olsen
發布: 2025-02-07 11:40:11
原創
483 人瀏覽過

Intellij Idea簡化了春季啟動的開發,使其成為Java開發人員的最愛。 它的慣例與配置方法最小化了樣板代碼,從而使開發人員可以專注於業務邏輯。本教程演示了兩種用於在Intellij Idea中創建和運行基本春季啟動應用程序的方法。

>這種強大的IDE促進了具有必要依賴項的項目創建,並導入現有的Spring Boot項目。 在開始之前,請確保您有:

    安裝了Java Development套件(JDK)。
  • Intellij Idea已安裝和配置。
  • 對Java和Spring概念的基本理解。
  • 在Intellij Idea
中設置您的Spring Boot Project

>步驟1:Intellij Ideas安裝

>訪問Intellij Idea網站:

https://www.php.cn/link/e8cb581442030021D62FD780FA674D

  1. > 單擊“下載”按鈕。
  2. 選擇您的首選版(社區或最終)。
  3. >下載並安裝,按照屏幕上的說明。
  4. 啟動Intellij Idea並完成初始設置。
  5. 步驟2:使用Spring Initializr
  6. 的項目生成項目
>導航到春季啟動式網站:

https://www.php.cn/link/link/bafd1b75c5f0ceb81050505050a853c9faa911

  1. > >指定項目詳細信息:
  2. 選擇您的Spring Boot版本。
    • 輸入組名稱。
    • 輸入偽影名(例如,
    • )。
    • > newProject選擇所需的依賴項(例如,春季Web)。
    • >
    單擊“生成”以將項目作為zip文件下載。
  3. >

How to Run Your First Spring Boot Application in IntelliJ?>步驟3:創建和配置Spring Boot Project

有兩種方法:使用Spring Initializr或直接在Intellij中創建Maven項目。

>

方法1:使用Spring Initializr

在Intellij Idea中打開下載的zip文件。

  1. 下創建
  2. 軟件包。
  3. > controllernewProject -> src/main/java軟件包中創建一個類
  4. >
  5. > ExampleC controller

How to Run Your First Spring Boot Application in IntelliJ?>將以下代碼添加到

>,確保您導入必要的註釋:>
  1. ExampleC>
  2. 方法2:在Intellij
package org.example.controller;

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

@Controller
public class ExampleC {
   @RequestMapping("/firstApp")
   @ResponseBody
   public String firstSpringApp(){
      return "Welcome!";
   }
}
登入後複製
登入後複製
中創建一個Maven項目
  1. >在Intellij中創建一個新的Maven項目。
  2. >在pom.xml>標籤中添加以下依賴項:> <dependencies>
package org.example.controller;

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

@Controller
public class ExampleC {
   @RequestMapping("/firstApp")
   @ResponseBody
   public String firstSpringApp(){
      return "Welcome!";
   }
}
登入後複製
登入後複製
>更新Maven配置(安裝階段)。

How to Run Your First Spring Boot Application in IntelliJ?

>將註釋添加到您的主應用程序類中。
    >
  1. @SpringBootApplication

How to Run Your First Spring Boot Application in IntelliJ?>添加

    方法。
  1. SpringApplication.run(Main.class, args); main
  2. 如圖1所述,創建
控制器類別的
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.5.3</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.5.3</version>
    </dependency>
</dependencies>
登入後複製
>
  1. >步驟4:運行應用程序ExampleC
  2. >單擊工具欄中的綠色“運行”按鈕。 控制台將顯示Tomcat啟動信息(端口8080)。

步驟5:查看結果

>通過How to Run Your First Spring Boot Application in IntelliJ?的瀏覽器訪問該應用程序。您應該看到“歡迎!”。

http://localhost:8080/firstApp

結論

How to Run Your First Spring Boot Application in IntelliJ?

Intellij Idea顯著簡化了Spring Boot應用程序的開發。 無論是使用Spring Initializr還是手動Maven設置,該過程都會簡化,從而有效地創建了可靠和可擴展的應用程序。 利用Intellij Idea的功能以及Spring Boot的“大會對結構”的功能,以進行最佳開發。

以上是如何在Intellij中運行第一個春季啟動應用程序?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
java可以做為web的後端嗎?
來自於 1970-01-01 08:00:00
0
0
0
安裝JAVA
來自於 1970-01-01 08:00:00
0
0
0
無法安裝java
來自於 1970-01-01 08:00:00
0
0
0
求救:JAVA加密的資料PHP解密
來自於 1970-01-01 08:00:00
0
0
0
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板