SpringBoot初体验

创建一个maven工程

引入springboot依赖

在pom.xml中引入

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

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

第一次编写springboot的小伙伴可能会遇到maven导入jar包失败:

  • 原因一:网不好,下一半卡住了,导致下载的文件缺失
    解决办法:找到导入jar包的文件夹,删除,然后maven重新加载
  • 原因二:下载太慢,一直build,等了好久没下载完,或者下载失败
    解决办法:利用国内的镜像,网上教程很多,配置maven中的setting.xml即可

主程序类、主入口类

package com.ly.controller;

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

//类的本身就是spring的一个组件

//程序的主入口
@SpringBootApplication
public class HelloWorld {
    public static void main(String[] args) {
        SpringApplication.run(HelloWorld.class,args);
    }
}

@SpringBootApplication:

  • Spring Boot应用标注在某个类上,说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法启动
  • 这是一个组合注解(由多个注解组合)

Controller类

package com.ly.controller;

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

@Controller
public class HelloController {
    @ResponseBody
    //接口:
    @RequestMapping("/hello")
    public String hello(){
        return "hello world!";
    }
}

pom.xml文件

//父项目
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
    <relativePath/>
</parent>

有了这个配置以后导入依赖默认不需要写版本;(但是也有例外)

导入依赖

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

spring-boot-starter是项目启动器,帮我们导入了web模块正常运行所依赖的组件

SpringBoot将所有的功能场景都抽取出来成为一个个组件,由spring-boot-starter来引入,根据需求引入场景启动器(例如这里就是web启动器)

application.properties

#更改项目的端口号
server.port=8081

启动项目

显示:Tomcat started on port(s): 8081 (http) with context path
请添加图片描述

网页访问

地址:localhost:8081/hello
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柠笙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值