spring-mvc配置异常问题FormattingConversionServiceFactoryBean

仅用作本人以后查看

原文地址:https://www.cnblogs.com/1315925303zxz/p/7504593.html

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 />



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC 是一个用于构建 Web 应用程序的框架,它基于 Spring 框架之上,提供了一套强大的处理请求、响应和视图的机制。下面是一个简单的 Spring MVC 配置示例: 1. 首先,你需要在 pom.xml 文件中添加 Spring MVC 相关的依赖项: ```xml <dependencies> <!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.9</version> </dependency> <!-- 其他依赖项 --> </dependencies> ``` 2. 创建一个配置类(例如 `WebConfig.java`),并配置 Spring MVC: ```java @Configuration @EnableWebMvc @ComponentScan(basePackages = "com.example.controller") // 指定控制器所在的包路径 public class WebConfig implements WebMvcConfigurer { @Override public void configureViewResolvers(ViewResolverRegistry registry) { registry.jsp("/WEB-INF/views/", ".jsp"); // 配置 JSP 视图解析器 } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**") .addResourceLocations("/static/"); // 配置静态资源访问路径 } } ``` 3. 创建一个控制器类(例如 `HelloController.java`): ```java @Controller public class HelloController { @RequestMapping("/") public String hello() { return "hello"; // 返回视图名称 } } ``` 4. 创建一个 JSP 视图文件(例如 `hello.jsp`): ```jsp <html> <body> <h1>Hello, Spring MVC!</h1> </body> </html> ``` 以上是一个简单的 Spring MVC 配置示例,你可以根据自己的需求进行进一步的配置和扩展。希望可以帮助到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值