一看就懂!Springboot配置内置Tomcat的虚拟路径映射

普通tomcat虚拟路径配置 

由于Springboot使用的是内置的tomcat,因此我们需要创建如下一个配置类,继承WebMvcConfigurer,重写其方法

addResourceHandler() : 指访问路径,相当于Setting.xml里的path属性

addResourceLocations() :前面要有"file:" 后面接上映射的路径,相当于Setting.xml里面的docBase属性

网上有一些是继承 WebMvcConfigurerAdapter 这个已经过时了。官方建议WebMvcConfigurer

@Configuration
public class WebMvcConfig implements WebMvcConfigurer{

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pic/**").addResourceLocations("file:D:/picture/");
    }
}

 

2020/5/26 补充 

访问路径应该是 【ip】:【端口】/【配置文件的context-path】/itemPic/【图片名称】

例如

在d:/picture/里放了一张叫test.png图片

假设 ip 为 localhost 端口为 8080 applaction配置文件配置的 server.servlet.context-path: /demo 虚拟路径如上文配置。

 

访问路径应该为 localhost:8080/demo/pic/test.png

这个是支持子文件夹的,如果test.png 在 D:/picture/2020/05/25/test.png

访问路径就应该为  localhost:8080/demo/pic/2020/05/25/test.png

相当于 localhost:8080/demo/pic/ 取代了 D:/picture /


有什么问题可以评论或者私信我,每日在线解(LIAO)疑(SAO)。

我是大誌,一位准备996的卑微码农🐶,觉得好用记得点赞收藏!!!

  • 19
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
Springboot使用内置tomcat作为默认的web容器,无需额外安装Tomcat,只需在服务部署时直接启动jar包即可。这是因为在spring-boot-starter-web中,默认导入的是tomcat,所以启动时使用的web容器就是tomcat。如果你想配置使用其他的web容器,比如Undertow,可以按照以下步骤进行操作: 1. 修改pom.xml文件,排除默认引入的spring-boot-starter-tomcat依赖,并添加Undertow依赖: ```xml <dependencies> <!-- 导入web场景启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 排除tomcat starter --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!-- 引入undertow --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency> </dependencies> ``` 通过这样的配置,你就可以使用Undertow作为springboot内置web容器了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot如何使用内嵌Tomcat](https://blog.csdn.net/u011523825/article/details/126114624)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot内置Tomcat配置和切换](https://blog.csdn.net/weixin_39158966/article/details/129745702)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大誌

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

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

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

打赏作者

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

抵扣说明:

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

余额充值