iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >解决springboot 2.x 里面访问静态资源的坑
  • 343
分享到

解决springboot 2.x 里面访问静态资源的坑

2024-04-02 19:04:59 343人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录SpringBoot 2.x 里面访问静态资源的坑首先看一下 自动配置类的定义:如果想要使用自动配置生效springBoot2.x过后static下的静态资源无法访问spring

springboot 2.x 里面访问静态资源的坑

Spring Boot的自定义配置类继承 WEBmvcConfigurationSupport 后,发现自动配置的静态资源路径

classpath:/META/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

不生效。

首先看一下 自动配置类的定义:

这是因为在 springboot的web自动配置类 WebMvcAutoConfiguration 上有条件注解


@ConditionalOnMissingBean(WebMvcConfigurationSupport.class) 

这个注解的意思是在项目类路径中 缺少 WebMvcConfigurationSupport类型的bean时改自动配置类才会生效,所以继承 WebMvcConfigurationSupport 后需要自己再重写相应的方法。

如果想要使用自动配置生效

又要按自己的需要重写某些方法,比如增加 viewController ,则可以自己的配置类可以继承 WebMvcConfigurerAdapter 这个类。不过在spring5.0版本后这个类被丢弃了 WebMvcConfigurerAdapter ,虽然还可以用,但是看起来不好。



@Configuration
public class BeanConfiguration implements WebMvcConfigurer {
    @Bean
    public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FaiL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setDateFORMat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        mapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        mapper.setDefaultPropertyInclusion(JSONInclude.Include.ALWAYS);
        converter.setObjectMapper(mapper);
        return converter;
    }
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //将我们定义的时间格式转换器添加到转换器列表中,
        //这样jackson格式化时候但凡遇到Date类型就会转换成我们定义的格式
        converters.add(jackson2HttpMessageConverter());
        // 添加字符串转换,否认如果返回字符串,则会报异常,其他converter 
        // 参考:org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#aDDDefaultHttpMessageConverters
        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
        stringHttpMessageConverter.setWriteAcceptCharset(false);  // see SPR-7316
        converters.add(stringHttpMessageConverter);
    }
}

SpringBoot2.x过后static下的静态资源无法访问


package com.example.thymeleaf.commons; 
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.ResourceHandlerReGIStry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 

@Component
public class WebMvcConfig implements WebMvcConfigurer {
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: 解决springboot 2.x 里面访问静态资源的坑

本文链接: https://www.lsjlt.com/news/133212.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作