springboot 日常采坑

static目录的访问

情景

  • 项目中需要做一个模板文件下载的功能,可以采用将文件流写入response,然后返回response这种方式。但本次直接使用的a标签,地址指向目标文件路径,从而实现文件下载。

问题

  • 于是需要访问静态文件,springboot中将静态文件放置在resource下的static中,templates用来存放html页面文件。但访问的时候,直接报404。静态文件访问不到。

解决方式

需要加一个拦截器,将对应的请求映射到静态目录中。

@Configuration
@EnableWebMvc
@ComponentScan
public class InterceptorConfiger implements  WebMvcConfigurer {

    private ApplicationContext applicationContext;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/");
        registry.addResourceHandler("/templates/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/templates/");
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

这样静态目录下的资源文件就可以直接被浏览器访问到。

引发问题

添加拦截器后,发现后台传向前台的时间类型数据,没有被json格式化,直接显示成了毫秒数。而时间格式化,是在application.yml中配置的。

spring:
  application:
    name: xxx
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8

也就是该配置失效了。

解决问题2

虽然不知道具体原理是什么,但是测试发现就是添加了拦截器所以导致时间格式转换除了问题。解决方案便是在拦截器中添加对时间格式的处理。

@Configuration
@EnableWebMvc
@ComponentScan
public class InterceptorConfiger implements  WebMvcConfigurer {

    private ApplicationContext applicationContext;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/");
        registry.addResourceHandler("/templates/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/templates/");
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
                .indentOutput(true)
                .dateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
                .modulesToInstall(new ParameterNamesModule()); //JDK8 新特性,可选择多个模块
        converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
    }
}

如此配置后,返回数据的时间格式以正确的json格式展示。

问题3

然而问题并没有结束,静态文件可以正常下载,服务器返回数据格式也正确,但是下载下来的文件打不开。(一脸懵逼)点开下载的excel文件,提示文件破损。原来是maven打包配置出了问题。详情参见

于是将maven中的filter改为false即可。

 <build>
        <finalName>${project.artifactId}</finalName>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.sql</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources/</directory>
                <filtering>false</filtering>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.ftl</include>
                    <include>**/*.xlsx</include>
                    <include>**/*.xls</include>
                    <include>**/*.json</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章