02-Springboot项目helloworld构建

本文介绍了如何使用Springboot 2.3.0快速构建一个简单的Web项目,包括创建Maven项目、添加web starter依赖、编写主启动类和HelloController,以及启动项目的步骤。
摘要由CSDN通过智能技术生成

上一篇:01-Springboot优点&缺点https://blog.csdn.net/fsjwin/article/details/109698409
Springboot可以快速构建出一个web项目,仅需要一下几步:

1. 创建项目

创建细节在此非重点哦!
在这里插入图片描述

在这里插入图片描述

2. pom文件

<?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>com.yuhl</groupId>
    <artifactId>springboot-20201114</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>01_first</module>
     
    </modules>

    <!--https://docs.spring.io/spring-boot/docs/2.4.0/reference/html/getting-started.html#getting-started-first-application-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
    </parent>


    <dependencies>
    引入web模块
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!--打包的插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  1. 使用springboot2.3.0
    在这里插入图片描述
  2. 使用springweb仅需引入web的starter即可
    在这里插入图片描述
    到此环境已经准备好了,使用起来是不是超级简单。

3. 主启动类FirstSpringBootApplication

在这里插入图片描述

package com.yuhl;

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

/**
 * @author yuhl
 * @Date 2020/11/14 8:54
 * @Classname FisrSpringBootApplication
 * @Description TODO
 */
@SpringBootApplication
public class FirstSpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(FirstSpringBootApplication.class, args);
    }
}

主启动类不可少,他是整个程序的入口。
注意:
1. @SpringBootApplication注解必不可少。
2. pringApplication.run(FirstSpringBootApplication.class, args);这一句就可使程序跑起来了。

4. 控制器HelloController

package com.yuhl;


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

/**
 * @author yuhl
 * @Date 2020/11/14 8:54
 * @Classname controller
 * @Description TODO
 */
@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello() {
        return "hello";
    }
}

5. 启动项目

在这里插入图片描述
启动成功,日志如下:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.0.RELEASE)

2020-11-15 08:11:35.095  INFO 6816 --- [           main] com.yuhl.FirstSpringBootApplication      : Starting FirstSpringBootApplication on LAPTOP-SGL4V58K with PID 6816 (D:\Idea2020Projects\springboot-20201114\01_first\target\classes started by yuhl in D:\Idea2020Projects\springboot-20201114)
2020-11-15 08:11:35.100  INFO 6816 --- [           main] com.yuhl.FirstSpringBootApplication      : No active profile set, falling back to default profiles: default
2020-11-15 08:11:36.141  INFO 6816 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-11-15 08:11:36.148  INFO 6816 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-11-15 08:11:36.148  INFO 6816 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.35]
2020-11-15 08:11:36.250  INFO 6816 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-11-15 08:11:36.250  INFO 6816 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1036 ms
2020-11-15 08:11:36.383  INFO 6816 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-11-15 08:11:36.505  INFO 6816 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-11-15 08:11:36.512  INFO 6816 --- [           main] com.yuhl.FirstSpringBootApplication      : Started FirstSpringBootApplication in 1.955 seconds (JVM running for 3.975)

6. 页面访问正常

在这里插入图片描述

7. 总结

创建boot项目的步骤

  1. 创建maven项目
  2. 引入web的starter
  3. 写主启动类添加注解@SpringbootApplication注解
  4. 启动即可
  5. 注意点:选择需要使用的boot版本即可。
    下一篇:03-Springboot配置文件application.yml || application.properties https://blog.csdn.net/fsjwin/article/details/109699856
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值