Info模式下的隐形杀手(SpringMVC同时使用<mvc:resources.../>和FormattingConversionServiceFactoryBean时出现的问题)

       今天早上手贱把项目误删了,不得不去SVN上去乞求了。我个人习惯项目运行的时候是debug模式跑着,但是,问题来了,启动竟然抛点异常。。。。。可是上周还好好的,让我有点怀疑人生了。但是还有一个但是,我把日志模式改为info模式,这个贱贱的错误又隐藏起来了,项目一切正常运行,是没问题的。声明一点啊,这个错误不是跟日志的模式有关。

       出现问题的根源,就是springmvc框架加载项目的时候,同时使用了加载静态资源的<mvc:resources>和定义了全局日期转换器。

1     <!-- 4、加载项目中的静态资源 -->
2     <mvc:resources location="/js/" mapping="/js/**" />
3     <mvc:resources location="/images/" mapping="/images/**" />
4     <mvc:resources location="/css/" mapping="/css/**" />
5     <mvc:resources location="/" mapping="/*.html" />
6     <!-- 日期统一转换 -->
7     <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
8     </bean>
9     <mvc:annotation-driven conversion-service="conversionService" />

 以这种骚姿态启动项目出现的报错信息,各位先有个印象:

 1 2017-09-11 12:59:42 3240 [localhost-startStop-1] DEBUG o.s.beans.TypeConverterDelegate - Original ConversionService attempt failed - ignored since PropertyEditor based conversion eventually succeeded 
 2 org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.util.ArrayList<?>] to type [java.util.List<org.springframework.core.io.Resource>] for value '[/]'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.util.ArrayList<?>] to type [org.springframework.core.io.Resource]
 3     at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41)
 4     at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:192)
 5     at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:173)
 6     at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:576)
 7     at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:603)
 8     at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:203)
 9     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1531)
10     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1490)
11     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1230)
12     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
13     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
14     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
15     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
16     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
17     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
18     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:778)
19     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
20     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
21     at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667)
22     at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:633)
23     at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:681)
24     at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:552)
25     at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493)
26     at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
27     at javax.servlet.GenericServlet.init(GenericServlet.java:158)
28     at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1282)
29     at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1195)
30     at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1085)
31     at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5318)
32     at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5610)
33     at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
34     at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1572)
35     at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1562)
36     at java.util.concurrent.FutureTask.run(FutureTask.java:262)
37     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
38     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
39     at java.lang.Thread.run(Thread.java:744)
40 Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.util.ArrayList<?>] to type [org.springframework.core.io.Resource]
41     at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:313)
42     at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195)
43     at org.springframework.core.convert.support.CollectionToCollectionConverter.convert(CollectionToCollectionConverter.java:87)
44     at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:35)
45     ... 36 common frames omitted

      错误提示中最核心的部分是:Failed to convert from type [java.util.ArrayList<?>] to type [java.util.List<org.springframework.core.io.Resource>] for value '[/WEB-INF/statics/js/]'翻译出来就是:

      尝试将'[/WEB-INF/statics/js/]从java.util.ArrayList<?> 转换到java.util.List<org.springframework.core.io.Resource>的时候失败了。

【为什么会出现这个问题?由于本人能力有限,还没有真正的了解到具体说法,如朋友你知底,请留言共勉,万分感谢】

 但是出现问题我们必须以最快的速度干掉它,那么解决办法我给各位提供了2种(既然是不能用这种方式同时出现,那么我就只允许他们只出现一种):

1、去除全局日期转换器

这种方式的配置呢,我们主要就是去解决项目中pojo类中日期属性的格式化问题,在这儿我们也可以不用这种方式去转换,而是通过在对象的时间属性上添加注解去格式化,具体看码:

 1 public class MortgagerInfo implements Serializable{
 2     
 3     private String id;                            //主键
 4     private String vin;                         //VIN码
 5     private String mortgagerName;                //抵押权人
 6     private String masterMortgagerNumber;        //主合同号
 7     private String mortgagerNumber;                //抵押合同号
 8     private Double mortgagerCost;                //抵押金额
 9     @DateTimeFormat(pattern="yyyy-MM-dd")
10     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
11     private Date mortgagerTime;                    //抵押登记日期
12     private String mortgagerImgUrl;                //抵押信息图片
13     @DateTimeFormat(pattern="yyyy-MM-dd")
14     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
15     private Date relieveTime;                    //解除日期
16     private Integer mortgagerStatus;            //抵押标记
17     private String approvalAuthority;            //批准机关

2、不要使用<mvc:resources>标签去加载静态资源,而是用以下方式加载,意思就是没有映射到的URL请求交给默认的web容器中的servlet进行处理:

 1    <!-- 4、加载项目中的静态资源 -->
 2     <!-- <mvc:resources location="/js/" mapping="/js/**" />
 3     <mvc:resources location="/images/" mapping="/images/**" />
 4     <mvc:resources location="/css/" mapping="/css/**" />
 5     <mvc:resources location="/" mapping="/*.html" /> -->
 6     <!-- 日期统一转换 -->
 7     <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
 8     </bean>
 9     <mvc:annotation-driven conversion-service="conversionService" />
10     
11     <mvc:default-servlet-handler />

现在用debug启动,错误消失(走到这儿可别忘了博主在文中请教的问题,留言区不见不散)。

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏wannshan(javaer,RPC)

ConcurrentHashMap 锁分段 源码分析

看ConcurrentHashMap下几个属性: /** * The default concurrency level for this table...

42560
来自专栏向治洪

React Native调用Android相机图库

概述 在很多的React Native开发中,我们需要调用原生的api实现调用相机和图库的功能,网上用的最多的开源库如:react-native-image-p...

32590
来自专栏木宛城主

Fix Backup Database is terminating abnormally When performing a Farm Backup

Problem I am trying to backup SharePoint 2013 Farm Automatically with PowerShell...

21570
来自专栏ml

caffe源码学习之Proto数据格式【1】

前言:   由于业务需要,接触caffe已经有接近半年,一直忙着阅读各种论文,重现大大小小的模型. 期间也总结过一些caffe源码学习笔记,断断续续,这次打算系...

69080
来自专栏逆向与安全

APK加固之类抽取分析与修复

隐藏dex文件:通过对目标DEX文件进行整体加密或压缩方式把整个dex转换为另外一个文件存放在assets文件夹中或者其它地方,然后利用类加载器技术进行内存解密...

43500
来自专栏happyJared

Spring Boot2集成Elasticsearch、PostgreSQL遇到的问题

  这个错误确实有点奇怪,不过好在Github上已经有相关Issue,有兴趣的可以去看看,该问题的解决方法是添加配置项:spring.jpa.propertie...

38440
来自专栏BinarySec

一些pwn题目的解题思路[pwnable.kr] II

目录 以下是solution的目录 #mistake #shellshock #coin1 #blackjack #lotto #cmd1 Other 一些pw...

41850
来自专栏向治洪

React Native调用Android相机图库

概述 在很多的React Native开发中,我们需要调用原生的api实现调用相机和图库的功能,网上用的最多的开源库如:react-native-image-p...

25750
来自专栏我是攻城师

ElasticSearch之Java Api聚合分组实战

49260
来自专栏Sorrower的专栏

用MediaPlayer做个带进度条可后台的音乐播放器

27940

扫码关注云+社区

领取腾讯云代金券