天氣一天比一天變的涼快了,而我一天天踩的坑更加賤了,首先在北京向各位問好,也給身邊獻身教育事業的親朋好友們補上一句節日快樂!
今天早上手賤把項目誤刪了,不得不去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啟動,錯誤消失(走到這兒可別忘了博主在文中請教的問題,留言區不見不散)。
