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启动,错误消失(走到这儿可别忘了博主在文中请教的问题,留言区不见不散)。

 

posted @ 2017-09-11 13:34  禁心尽力  阅读(2593)  评论(0编辑  收藏  举报