简体   繁体   English

为什么不春季使用我的转换器 <String, Date> ?

[英]Why isn't spring using my Converter<String, Date>?

I have this in my applicationContext.xml 我的applicationContext.xml中有这个

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <set>
            <bean class="mycompany.AsOfDateConverter"/>
            <bean class="mycompany.CustomerConverter"/>
            <bean class="mycompany.FooConverter"/>
        </set>
    </property>
</bean>

AsOfDateConverter looks like AsOfDateConverter看起来像

public class AsOfDateConverter implements Converter<String, Date> {
    @Override
    public Date convert(String source) {
        if(source == null) return new Date();
        //... else parse date. not shown.
    }

}

But Spring never picks up my DateConverter. 但是Spring从来没有拿起我的DateConverter。 Instead I get this 相反,我得到这个

org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date]: no matching editors or conversion strategy found
    at org.springframework.beans.SimpleTypeConverter.convertIfNecessary(SimpleTypeConverter.java:53)
    at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:534)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestParam(HandlerMethodInvoker.java:506)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:339)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:170)

Two solutions needed: a) Why isn't it using my converter? 需要两个解决方案:a)为什么不使用我的转换器? b) if date is null then can converter still call my converter? b)如果日期为空,那么转换器可以继续呼叫我的转换器吗?

I have all this working with PropertyEditors but wanted to port to Converters. 我已经将所有这些与PropertyEditors一起使用,但想移植到Converters。

But I can't figure out why Spring MVC does not use my DateConverter. 但是我不知道为什么Spring MVC不使用我的DateConverter。 I have it implemented so that if source 我实现了它,以便如果源

I also got 我也有

2012-06-26 12:41:55,215 DEBUG DefaultHandlerExceptionResolver - Resolving exception from handler [public org.springframework.http.ResponseEntity<Resource<java.lang.Object>> StatisticsController.getStats(java.util.Date)]: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date]: no matching editors or conversion strategy found 

when trying to call to my controller method 尝试调用我的控制器方法时

public ResponseEntity<Resource<Object>> getStats(@RequestParam Date fromDate)

Reading about ConversionServiceFactoryBean on 阅读有关ConversionServiceFactoryBean的信息

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html

indicates that date support requires JODA time which we didn't want. 表示日期支持需要我们不需要的JODA时间。 So I tried to add my own converter (that implements Converter) to ConversionServiceFactoryBean 因此,我尝试将自己的转换器(实现Converter)添加到ConversionServiceFactoryBean

<bean id="conversionService"
      class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <list>
            <bean class="MyStringToDateConverter"/>
        </list>
    </property>
</bean>

and also bound this to the mvc-annotation driven tag: 并将其绑定到mvc-annotation驱动的标签:

<mvc:annotation-driven  conversion-service="conversionService"/> 

But I never got my converter to kick in. I always got "no matching editors or conversion strategy found". 但是我从来没有加入过转换器。我总是得到“找不到匹配的编辑器或转换策略”。

As indicated above I was hoping to use the Spring MVC 3.x style of conversion service but I had to go for the workaround with the old approach with init binder. 如上所述,我希望使用Spring MVC 3.x样式的转换服务,但是我不得不采用带有初始绑定程序的旧方法进行变通。

/**
 * I would claim this is should not be needed in Spring 3.x with ConversionServiceFactoryBean
 * @param binder
 */
@InitBinder
public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}

This worked immediately. 这立即起作用。

I can give you a hint: The Exception is thrown from the class ´org.springframework.beans.SimpleTypeConverter´. 我可以给你一个提示:异常是从“ org.springframework.beans.SimpleTypeConverter”类抛出的。 This class belongs to the PropertyEditor Support (Framwork) but not to the ConversionService Framework. 此类属于PropertyEditor支持(Framwork),但不属于ConversionService Framework。

So it seam that mixing both does not work like you want, or the ConversionService is not enabled: 因此,可能无法同时使用这两种功能,或者未启用ConversionService:

5.5.5 Configuring a ConversionService 5.5.5配置ConversionService

If no ConversionService is registered with Spring, the original PropertyEditor-based system is used. 如果未向Spring注册任何ConversionService,则使用原始的基于PropertyEditor的系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM