使用idea创建第一个springboot项目 及 如何启动springboot项目 及 如何指点端口和上下文

一 使用idea创建第一个springboot项目

使用IDEA创建一个Spring Boot项目有很多方法,这里介绍使用Maven和Spring Initializr这两种方法来创建一个新Spring Boot项目

方式一:使用Maven新建Spring Boot项目

1. File --> New --> Project... --> Maven ,如下图所示

Project SDK下拉列表框中选择前面安装的 Java1.8,如果下拉列表框中不存在Java 1.8,可以单击New按钮,找到安装Java的位置,选择它。至于Maven中的archetype,不用勾选。然后单击“Next”

2. 给新工程起名spring-boot-hello,配置GAV

 3.配置pom.xml

使用Maven,通过导入Spring Boot的 starter 模块,可以将许多程序依赖包自动导入工程中。使用Maven的parent POM,还可以更容易地管理依赖的版本和使用默认的配置,工程中的模块也可以很方便地继承它。使用如下代码清单所示的简单maven配置,基本上就能为一个使用Sping Boot开发框架的Web项目开发提供所需的相关依赖。

<?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>

    <groupId>org.example</groupId>
    <artifactId>spring-boot-hello</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.12</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

这里只使用了一个依赖配置 spring-boot-starter-web 和一个 parent 配置 spring-boot-starter-parent。
 

4.创建Controller

如下是官方提供的一个最简单的Web示例程序,这个实例只使用了几行代码,虽然简单,但实际上这已经算是一个完整的Web项目了。
 

详情如下:

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class ApplicationDemo {

    @RequestMapping("/")
    String home() {
        return "hello";
    }

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

 这个简单实例,首先是一个Spring Boot应用的程序入口,或者叫作主程序,其中使用了一个注解@SpringBootApplication来标注它是一个Spring Boot应用,main方法使它成为一个主程序,将在应用启动时首先被执行。其次,注解 @RestController 同时标注这个程序还是一个控制器,如果在浏览器中访问应用的根目录,它将调用 home 方法,并输出字符串:hello。

5.在IDEA环境中运行,浏览器访问效果如下

注意:如上只能在IDEA环境中运行,那么如何打包发布到环境外运行?需要在项目maven配置增加一个发布插件来实现。增加一个打包插件:spring-boot-maven-plugin

如下所示:

<?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>

    <groupId>org.example</groupId>
    <artifactId>spring-boot-hello</artifactId>
    <version>1.0-SNAPSHOT</version>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.12</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

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

</project>

打包成功后,在工程的target目录中将会生产jar文件spring-boot-hello-1.0-SNAPSHOT.jar。在命令行窗口中切换到target目录中,运行如下指令,就能启动应用。

java -jar spring-boot-hello-1.0-SNAPSHOT.jar

方式二: 使用Spring Initializr 新建Spring Boot项目

1. File --> New --> Project... --> Spring Initializr ,如下图所示

指定jdk,Choolse Initializr Service URL 保持默认即可。然后,next.

2. 在弹出的 Project Metadata 窗口中,Version: 默认是 0.0.1-SNAPSHOT ,我习惯指定为 1.0.0

3. Spring Boot版本选择2.1.13 ,依赖只选择 一个 Web --> Spring Web 即可。

4. 指定工程路径,Finish

5. 选择 New Window

6. 生成的项目结构如下

其中 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.1.13.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.hello</groupId>
    <artifactId>hello-world</artifactId>
    <version>1.0.0</version>
    <name>hello-world</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>

HelloWordApplication 这个类内容如下所示:

package com.example.hello.helloworld;

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

@SpringBootApplication
public class HelloWorldApplication {

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

}

7. 在com.example.hello.helloworld 包下创建一个Controller

给HelloController 类添加 @RestController注解,并在类里面创建一个 hello()方法,给hello()方法添加 @GetMapping注解

package com.example.hello.helloworld;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello World! 这是我创建的第一个SpringBoot项目。";
    }
}

至此,咱们的第一SprinBoot项目使用idea创建完成。

二 如何启动springboot项目

启动方式1:

启动服务,点击如图所示右上角绿色三角形启动服务。

启动说明:通过默认配置启动的是8080端口

浏览器访问网址 http://localhost:8080/hello , 效果如下

启动方式2:

找到 HelloWorldApplication 类

三 如何指点端口和上下文

若想指定端口和上下文(默认端口是8080),可在 resources 目录下找到一配置文件 application.properties (默认该文件为空文件,文件没有任何内容)

在 application.properties 文件中指定 端口 号 和上下文。

application.properties 文件内容 如下:

server.port=9090
server.servlet.context-path=/say-hello

eg:

也可以使用application.yaml 文件来代替 application.properties 文件。

application.yaml文件中指定端口和上下文写法如下:

server:
  port: 9090
  servlet:
    context-path: /say-hello

eg:

启动服务:

访问url:    http://localhost:9090/say-hello/hello    效果如下:

注意:

如果将默认生成的 application.properties文件删掉。服务仍可正常启动,不过端口和上下文变成了默认的(其实resources整个文件夹都可以删掉)。如下图:

### 回答1: 使用IDEA创建一个Spring Boot项目可以按照以下步骤进行: 1. 打开IDEA,选择 “Create New Project” 进入新建项目页面。 2. 在页面中选择 “Spring Initializr” 作为项目类型。 3. 选择项目名称、项目路径、项目类型等基本信息。 4. 在 “Dependencies” 页面选择需要用到的依赖,如Spring Boot Starter、Spring Web、Spring Data JPA等。 5. 点击 “Next” 完成项目创建。 接下来,您可以在IDEA中对Spring Boot项目进行开发、调试和测试等操作。 ### 回答2: Spring Boot 是基于 Spring 开发的一种应用程序开发框架,其中 Java 的依赖性配置被自动处理,并提供了一种命令行界面,可以用来创建、运行和测试应用程序。使用 IntelliJ IDEA 工具可以轻松地创建一个 Spring Boot 项目。 步骤如下: 第一步:创建一个新的项目。 首先,打开 IntelliJ IDEA,并点击 "Create New Project"。在弹出的对话框中,选择 Spring Initializr。这是一个可从各种项目模板中选择的启动器,这里选择了“Spring Web”。 第二步:选择项目的属性。 添加项目名称,然后指定项目的保存位置。选择 Maven 作为依赖管理方案,并选择适当的 Java 版本。此处选择的是 Java 11。 第三步:设置项目模板。 选择 Spring Boot 项目模板,然后选择 Web 模板。点击下一步按钮。 第四步:选择 Spring Boot 依赖项。 选择适合应用程序的 Spring Boot 依赖项。在这里,选择 Web、Thymeleaf 和 DevTools。这些依赖项提供了必要的功能,如 Web 应用程序的构建,视图和模板的管理,以及开发工具的支持。 第五步:创建项目创建项目后,你可以浏览项目文件结构并编辑必要的文件。此时,你应该能够看到与创建Spring Boot 项目有关的所有文件和依赖项。需要注重的是,应该始终注意项目结构和整体代码质量。 最后,你可以运行应用程序并启动 web 服务器,以便进行测试和调试。这是通过点击工具栏上的运行按钮和选择你的 Spring Boot 应用程序来实现的。至此,整个过程已经完成,你可以根据需要调整和修改项目。 ### 回答3: 使用IntelliJ IDEA创建一个Spring Boot项目非常简单,遵循下列步骤: 步骤 1:创建项目 打开IntelliJ IDEA,点击File -> New -> Project,选择Spring Initializr。 在Spring Initializr页面中,你需要选择项目的不同选项,例如项目语言、Spring Boot版本、项目元数据等等。 完成项目创建后,你将在IDEA中看到一个名为“pom.xml”的Maven项目文件。这将是我们构建新项目的核心。 步骤 2:添加依赖 Spring Boot集成了多种依赖,通常你可以很容易地在pom.xml文件中一次性添加它们。 例如,为添加Spring Web依赖,你可以像下面这样添加一行代码到pom.xml中: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 这将添加Spring的web依赖到你的项目中,让你可以开始编写Web应用程序。 步骤 3:编写代码 现在,开始编写你的Spring Boot应用程序了!例如,编写一个简单的Hello World应用程序,显示在网页上“Hello, World!”。 在你的IDEA工程中的src/main/java目录下,创建一个名称为“com.example.demo”的Java package,并在其下创建一个名为“Apllication.java”的Java类,代码如下: ```java package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @RequestMapping("/") @ResponseBody String home() { return "Hello, World!"; } } ``` 此时,在浏览器中访问http://localhost:8080,你将看到“Hello, World!”这个网页。 步骤 4:运行项目 最后,我们需要测试一下Spring Boot项目,为此,我们需要在IDEA启动它。你可以通过按下 Control + Shift + R 打开或者右键点击主类选择“Run Application”来运行你的应用程序。 当你成功运行应用程序时,你的控制台日志将输出与启动有关的信息。 至此,你已经成功地使用IntelliJ IDEA创建了一个简单的Spring Boot项目,并创建了一个Hello World应用程序!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值