Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register a custom conversion service while retaining the defaults?

In a spring-mvc 3.2.RELEASE project I'd like to use org.springframework.data.repository.support.DomainClassConverter to easily get me entities injected.

It works fine when using this config:

<beans:bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean" />

<beans:bean id="conversionService"
    class="org.springframework.core.convert.support.DefaultConversionService" /> 

<beans:bean
    class="org.springframework.data.repository.support.DomainClassConverter">
    <beans:constructor-arg ref="conversionService" />
</beans:bean>

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

But then Spring isn't loading the formatter for dealing with Joda time types and i get "Failed to convert property value of type java.lang.String to required type org.joda.time.LocalDate for property"

Using only

<annotation-driven/>

The Joda conversion works but not the entity injection.

How do you wire it upp so both work?

like image 264
NA. Avatar asked Dec 20 '12 11:12

NA.


People also ask

What is converter in Java?

Converter is an interface describing a Java class that can perform Object-to-String and String-to-Object conversions between model data objects and a String representation of those objects that is suitable for rendering. Converter implementations must have a zero-arguments public constructor.


1 Answers

Not sure if this answers the question, but I came across a similar problem and this is how I resolved it.

I had implemented a custom converter and conversion service using the documentation

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

The result was what @NA described - this loses the default joda time support and the following definition in an unrelated controller no longer works

@PathVariable(value="day") @DateTimeFormat(pattern=DATE_FORMAT) LocalDate date

The solutions was instead of using org.springframework.context.support.ConversionServiceFactoryBean, I began using org.springframework.format.support.FormattingConversionServiceFactoryBean.

like image 185
Craig Swing Avatar answered Sep 28 '22 00:09

Craig Swing