前言
当我们使用springboot进行开发时,因为已经内嵌了tomcat,我们不用手动配置tomcat即可运行。文章源自随机的未知-https://sjdwz.com/11167.html
我们的项目在写完后,会在服务器上的tomcat内运行,所以我们测试时,也需要在本地配置tomcat环境。本文便介绍了如何使用IDEA配置tomcat环境,并把springboot项目在配置的tomcat内运行。文章源自随机的未知-https://sjdwz.com/11167.html
创建springboot项目
项目创建
首先指定项目名,路径,打war包;文章源自随机的未知-https://sjdwz.com/11167.html
选择Next;文章源自随机的未知-https://sjdwz.com/11167.html
这里我们仅测试web项目,只选择Spring Web依赖就可以了;文章源自随机的未知-https://sjdwz.com/11167.html
点击Finsh。文章源自随机的未知-https://sjdwz.com/11167.html
配置文件
在配置文件添加如下内容:文章源自随机的未知-https://sjdwz.com/11167.html
spring.application.name=tomcatconfig
server.port=9999
server.servlet.context-path=/tomcatconfig
项目结构
项目结构如下:文章源自随机的未知-https://sjdwz.com/11167.html
添加测试Controller
创建包
文章源自随机的未知-https://sjdwz.com/11167.html
创建测试Controller
在新建的包右键
文章源自随机的未知-https://sjdwz.com/11167.html
在测试类里添加如下代码文章源自随机的未知-https://sjdwz.com/11167.html
package com.sjdwz.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @Description 测试Controller * @Date 2022/3/12 * @Created by 随机的未知 sjdwz.com */ @RestController @RequestMapping("/mytest") public class TestController { @GetMapping("/test") public String test(){ return "test tomcat config"; } }
项目启动

选择springboot启动类,点击运行。文章源自随机的未知-https://sjdwz.com/11167.html
在浏览器输入http://localhost:9999/tomcatconfig/mytest/test文章源自随机的未知-https://sjdwz.com/11167.html
回车后可得如下图。文章源自随机的未知-https://sjdwz.com/11167.html
IDEA配置tomcat
选择启动类位置的下拉三角;文章源自随机的未知-https://sjdwz.com/11167.html
选择Edit Configurations...文章源自随机的未知-https://sjdwz.com/11167.html
点击左上角 **+**;文章源自随机的未知-https://sjdwz.com/11167.html
往下滑,找到Tomcat Server文章源自随机的未知-https://sjdwz.com/11167.html
选择后显示如下图:文章源自随机的未知-https://sjdwz.com/11167.html
Server处的配置
1位置填写一个名字;文章源自随机的未知-https://sjdwz.com/11167.html
选择2位置,配置本地的tomcat的目录;文章源自随机的未知-https://sjdwz.com/11167.html
Deployment处的配置
选择Deployment,然后点击**+**;文章源自随机的未知-https://sjdwz.com/11167.html
选择Artifact;文章源自随机的未知-https://sjdwz.com/11167.html
选择没有exploaded的这一项;文章源自随机的未知-https://sjdwz.com/11167.html
如图:文章源自随机的未知-https://sjdwz.com/11167.html
这是项目访问路径;我们将他改成**/**文章源自随机的未知-https://sjdwz.com/11167.html
再看一下访问路径

点击ok文章源自随机的未知-https://sjdwz.com/11167.html
访问项目
选择刚配置好的tomcat文章源自随机的未知-https://sjdwz.com/11167.html
点击运行。文章源自随机的未知-https://sjdwz.com/11167.html
我们的项目可以通过http://localhost:8080/来访问;文章源自随机的未知-https://sjdwz.com/11167.html
刚才的测试方法可以通过http://localhost:8080/mytest/test来访问。文章源自随机的未知-https://sjdwz.com/11167.html
将项目跑起来,可以访问上述路径。文章源自随机的未知-https://sjdwz.com/11167.html