Spring boot 基础web入门搭建

本文详细介绍了如何从零开始搭建Spring Boot基础Web项目,包括创建简单项目、打包可执行jar和war包,以及如何配置和运行。内容涵盖Spring Boot项目结构、启动类配置、配置文件的应用,以及如何在不同容器中运行。

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

springBoot 基础入门


简介
spring boot 现在已经成为java 搭建微服务系统的基础技能,生态圈已经越发的完善;网上的资料也很丰富了,学习之余总结下,巩固巩固。一下是一系列的创建使用流程,更多的适用一些有过了解和使用过的人员。

知识点:
1. 创建一个简单的springboot web项目
2. 打包可执行jar 和传统war包
3. 熟悉springboot 项目的基本结构

源码

源码链接地址

下一节 Spring boot Banner和icon

Spring boot Banner和icon

开启springboot应用程序

环境

 JDK: 1.8+
 springboot version: 2.1.6.RELEASE
 maven 项目环境
 开发工具  idea

首先创建一个父pom工程project
在这里插入图片描述

1. 父工程

xml 中使用 集成

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

不 过 我 们 通 常 使 用 依 赖 管 理 的 方 式 使 用 s p r i n g b o o t \color{red}{不过我们通常使用依赖管理的方式使用springboot} 使使springboot
(本教程使用parent方式)

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
2. 创建 demo项目

new 一个module, 创建 maven项目, 这样我们继承了 父项目,也就依赖了 spring-boot-starter-parent
在这里插入图片描述
demo 项目 pom中 添加 spring-boot-starter-web的依赖

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

创建启动类,添加注解@SpringbootApplication

@SpringBootApplication
public class ApplicationStart {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationStart.class);
    }
}

运行 main 方法

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

2021-03-22 10:35:50.825  INFO 52572 --- [           main] c.x.springboot.demo.ApplicationStart     : Starting ApplicationStart on DESKTOP-N5K2GI1 with PID 52572 (F:\workSpaces\idea\xiaodu\spring-boot-all\com-xiaodu-springboot-demo-web\target\classes started by huayu in F:\workSpaces\idea\xiaodu\spring-boot-all)
2021-03-22 10:35:50.827  INFO 52572 --- [           main] c.x.springboot.demo.ApplicationStart     : No active profile set, falling back to default profiles: default
2021-03-22 10:35:51.973  INFO 52572 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-03-22 10:35:51.991  INFO 52572 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-03-22 10:35:51.991  INFO 52572 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.21]
2021-03-22 10:35:52.157  INFO 52572 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-03-22 10:35:52.157  INFO 52572 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1302 ms
2021-03-22 10:35:52.433  INFO 52572 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-22 10:35:52.572  INFO 52572 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-22 10:35:52.574  INFO 52572 --- [           main] c.x.springboot.demo.ApplicationStart     : Started ApplicationStart in 2.187 seconds (JVM running for 3.485)

启动成功,日志显示端口 8080

验证访问
在这里插入图片描述

配置文件

如上我们成功了启动了springboot 项目,现在我们使用配置文件, 配置端口

application.properties
resources 目录下创建application.properties 配置文件,并编辑
在这里插入图片描述
从新启动,访问,这时候端口发生了变化

使用ymal 文件

更多的时候我们使用yml 文件充当配置文件,层次结构更加明了
application.yml
server:
  port: 8171
spring:
  application:
    name: springboot-web-demo

编写controller

@RestController
@RequestMapping("/demo")
public class DemoController {

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

}

注 意 创 建 类 要 在 启 动 类 的 包 路 径 下 \color{#FF0000}{注意创建类要在启动类的包路径下}

启动访问 hello
在这里插入图片描述

3. 打包
创建可执行jar

添加maven插件

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

运行maven命令 或者使用idea工具直接点击

mvn package -DskipTests
生成target下的jar

运行jar
java -jar com.xiaodu.springboot-demo-web-1.0-SNAPSHOT.jar

打包war,使用传统容器运行

-pom文件修改
修改packaging 为war,并排除掉嵌入的tomcat

 <packaging>war</packaging>
 ...
  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <!--排除掉嵌入的tomcat容器-->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 添加tomcat容器,并设置scope provided-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

配置类

继承SpringbootServletInitializer, (没有特殊的配置也可以不重写configure方法)
@SpringBootApplication
public class ApplicationStart extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationStart.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(ApplicationStart.class);
    }
}

打包到tomcat运行

打包好的war,修改为demo.war,(自动生成的名字过长,tomcat默认path是项目名称),放到webapps目录下,然后去bin目录下startup.bat 点击启动, tomcat 默认端口8080,项目path为项目名称demo。
当然也可是在idea或者eclipse总配置server直接部署运行。

在这里插入图片描述

以上完成了打包运行。

如需切换容器,只需要修改依赖即可
例如:

切换嵌入式容器jetty
  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <!--排除掉嵌入的tomcat容器-->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
  <!--添加 jetty-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
            <scope>compile</scope>
        </dependency>

下一节:Spring boot Banner和icon

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值