SpringBoot的helloworld项目

1.工具说明:

使用idea+maven方式。项目的层次结构
在这里插入图片描述

2.新建springboot工程

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.在pom.xml中导入springboot入门程序所需的依赖坐标

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

4.编写springboot主程序代码

package com.example.MainApplication;


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

//标志主程序类,表明这是一个springboot应用
@SpringBootApplication
public class SpringBootMain {
    public static void main(String[] args) {
        //加载springboot主程序
        SpringApplication.run(SpringBootMain.class,args);
    }
}

5.编写controller层代码

package com.example.controller;

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

@Controller
public class helloController {

    @ResponseBody
    @RequestMapping("/hello")
    public String sayHello(){
        return "hello SpringBoot";
    }
}

6.回到第4步写的主程序,运行主程序

在这里插入图片描述

7.观察控制台,我们可以看到Tomcat已经启动了在这里插入图片描述

8.在浏览器发送hello请求,访问helloController中的sayHello方法

http://localhost:8080/hello

在这里插入图片描述

8.总结

这个入门程序就相当于使用了springmvc的功能,处理浏览器发送的请求。在controller层中的@ResponseBody和@RequestMapping两个注解就是springmvc的注解。

但跟使用springmvc实现处理hello请求不同的是,使用springboot不需要我们书写springmvc的配置文件,也没用导入springmvc的依赖(其实是被springboot的依赖封装起来的,可以点进去springboot的依赖坐标看的,方法就是按住ctrl键点击pom文件中的依赖)
简介,减少配置文件,这就是springboot的强大之处。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值