Springboot的初步入门:第一个程序Hello Spring

本文介绍了如何一步步搭建一个简单的Spring Boot项目。首先创建Maven项目,然后在pom.xml中引入Spring Boot的父工程及web、测试和Thymeleaf相关依赖。接着编写启动类和控制器,最后运行程序并在浏览器中验证结果。通过这些步骤,成功创建了一个运行在8080端口的Spring Boot应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:创建一个简单的mavne项目
在这里插入图片描述
创建一个简单的maven项如上图所示,这是一个最简单的maven项目

第二步:在pom.xml中添加springboot所需要的依赖

 <!--引入一个父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

	<dependencies>
        <dependency>  <!-- 这是启动web,使用tomcat作为默认的容器-->
            <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>

        <dependency>  <!--thymeleaf的依赖-->
            <groupId>org.springframework.boot </groupId>
            <artifactId>spring-boot-starter-thymeleaf </artifactId>
        </dependency>
    </dependencies>

    <build>   <!-- 打jar包的插件-->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

在pom.xml中引入依赖之后,项目的右边会出现sprigboot所需要的jar包
在这里插入图片描述

第三步:创建启动类
在这里插入图片描述

启动类的代码如下所示:

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

@Controller
public class HelloController {

        @RequestMapping("/hello") @ResponseBody
        public String hello(){
            return "Hello Springboot";
        }
}

第四步:创建控制层,controller
在这里插入图片描述

HelloController的代码如下所示:

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

@Controller
public class HelloController {

        @RequestMapping("/hello") @ResponseBody
        public String hello(){
            return "Hello Springboot";
        }
}

第五步:在Application中运行程序,在网页中输入localhost:8080/hello,页面如图所示,第一个springboot的程序创建成功
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值