当前位置: 首页 > news >正文

Springboot使用外部的Servelt容器(最简单的方式)

第一步、修改POM.xml文件

         将SpringBoot内置的Tomcat排除替换原有的部分依赖并添加api依赖

		<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></exclusion></exclusions></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId></dependency>

第二步、修改启动类

                需要继承SpringBootServletInitializer

package com.qcby.springbootusemvc;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;@SpringBootApplication
public class SpringbootusemvcApplication extends SpringBootServletInitializer {public static void main(String[] args) {SpringApplication.run(SpringbootusemvcApplication.class, args);}protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){return builder.sources(SpringbootusemvcApplication.class);}
}

第三步、添加controller

package com.qcby.springbootusemvc.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/exam")
public class ExampleController {/*** Springboot 使用外部的tomcat** @Author 刘庆亮* @Date 2025-07-22 16:26* @param: null* @return null*/@GetMapping("/getCity")public String getCityInfo() {return "杭州";}}

第四步、添加外部的Tomcat

                        

                         

 

 

 第五步、添加application.yaml

server:port: 18081servlet:context-path: /example-demo

第六步、启动(不许再启动类哪里启动一定要在下面标注的地方在启动!!!

最后一步、在网址搜索 http://localhost:8080/hub_demo/exam/getCity

代码已经放到远程仓库需要自取

地址https://gitee.com/liu-qing_liang/spring-boot-exclsion-tomcat

http://www.lryc.cn/news/596213.html

相关文章:

  • 1-bit AI 基础设施:第 1.1 部分 —— 在 CPU 上实现快速且无损的 BitNet b1.58 推理
  • ubuntu24.04安装CUDA、VLLM、Pytorch等并部署Qwen3-8B-AWQ【50系显卡通用】
  • proxmox 解决docker容器MongoDB创建报错MongoDB 5.0+ requires a CPU with AVX support
  • Leetcode力扣解题记录--第73题(矩阵置零)
  • Leetcode题解:209长度最小的子数组,掌握滑动窗口从此开始!!!
  • Vue中最简单的PDF引入方法及优缺点分析
  • Gradio, Streamlit, Dash:AI应用开发的效率之选
  • 配置https ssl证书生成
  • 拓展三字棋
  • ansible 批量 scp 和 load 镜像
  • 2025 年 7 月 21 日 AI 日报
  • 位运算符的妙用
  • 消息推送功能设计指南:精准触达与用户体验的平衡之道
  • OpenCV 零基础到项目实战 | DAY 1:图像基础与核心操作
  • Qt文件操作:读写文件的各种方法
  • 模运算常见定律
  • Java学习----Redis集群
  • Custom SRP - Draw Calls
  • Linux异常与信号处理
  • 11.【C语言学习笔记】指针(三)(回调函数、qsort排序函数、sizeof关键字和strlen函数)
  • Mixed Content错误:“mixed block“ 问题
  • 西门子 S7-1500分布式 I/O通信 :PROFINET IO 与 PROFIBUS DP核心技术详解(上)
  • 知识库搭建之Meilisearch‘s 搜索引擎-创建搜索引擎项目 测评-东方仙盟测评师
  • 【Godot4】状态栏组件StatusBar
  • python中 tqdm ,itertuples 是什么
  • RabbitMQ--批量处理
  • halcon手眼标定z方向实操矫正
  • VUE 中父级组件使用JSON.stringify 序列化子组件传递循环引用错误
  • 机器人氩弧焊保护气降成本的方法
  • Apache Ignite 的 SQL 功能和分布式查询机制